PHP棘手问题

发布于 2024-12-06 08:52:54 字数 997 浏览 5 评论 0原文

我有以下类结构:

class Parent
{
    public function process($action)
    {
        // i.e. processCreateMyEntity
        $this->{'process' . $action};
    }
}

class Child extends Parent
{
    protected function processCreateMyEntity
    {
        echo 'kiss my indecisive ass';
    }
}

我需要在 Child 类中编写一些统一的方法来处理几个非常相似的创建实体的操作。我无法更改 Parent::process 并且我需要从中调用这些方法。

首先想到的是神奇的 __call 方法。实体名称是从第一个 __call 参数解析的。所以结构变成:

class Parent
{
    public function process($action)
    {
        // i.e. processCreateMyEntity
        $this->{'process' . $action};
    }
}

class Child extends Parent
{
    protected function __call($methodName, $args)
    {
        $entityName = $this->parseEntityNameFromMethodCalled($methodName);
        // some actions common for a lot of entities
    }
}

但问题是 __call 不能像我需要的那样受到保护。我在 __call 方法的开头放置了一个 hack 方法调用,该方法通过 debug_backtrace 检查该方法是否在 Parent::process 内部调用,但这闻起来很糟糕。

有什么想法吗?

I have the following class structure:

class Parent
{
    public function process($action)
    {
        // i.e. processCreateMyEntity
        $this->{'process' . $action};
    }
}

class Child extends Parent
{
    protected function processCreateMyEntity
    {
        echo 'kiss my indecisive ass';
    }
}

I need to write some unified method in Child class to process several very similar actions for creating entities. I can't change the Parent::process and I need those methods to be called from it.

The first thing that comes to mind is magic __call method. The entity name is parsed from the first __call argument. So the structure turns to:

class Parent
{
    public function process($action)
    {
        // i.e. processCreateMyEntity
        $this->{'process' . $action};
    }
}

class Child extends Parent
{
    protected function __call($methodName, $args)
    {
        $entityName = $this->parseEntityNameFromMethodCalled($methodName);
        // some actions common for a lot of entities
    }
}

But the thing is that __call can't be protected as I need it. I put a hack method call at the beginning of __call method that checks via debug_backtrace that this method was called inside Parent::process, but this smells bad.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七七 2024-12-13 08:52:54

如果“几个”意味着 3 或 4,我可能会这样做:

protected function processThis()
{
  return $this->processThings();
}

protected function processThat()
{
  return $this->processThings();
}

protected function processThings()
{
  //common function
}

当然,有重复的代码,但它所做的立即有意义。有一些函数可以做类似的事情,而且很容易发现这一点。

If 'several' means 3 or 4, I'd probably just do something like:

protected function processThis()
{
  return $this->processThings();
}

protected function processThat()
{
  return $this->processThings();
}

protected function processThings()
{
  //common function
}

Sure, there is duplicate code, but what it does makes immediate sense. There are a handful of functions that do something similar, and it's easy to discover that.

可爱暴击 2024-12-13 08:52:54

我假设你的孩子是从父母那里延伸出来的。

那么你可以做的是:

public function process($action)
{
    $methods = get_class_methods($this);
    $action = 'process' . $action;
    if(in_array($action, $methods)){
        $this->{$action}()
    }
    else {
       die("ERROR! $action doesn't exist!");
    }
}

I am assuming your child extends from the parent.

Then what you could do is:

public function process($action)
{
    $methods = get_class_methods($this);
    $action = 'process' . $action;
    if(in_array($action, $methods)){
        $this->{$action}()
    }
    else {
       die("ERROR! $action doesn't exist!");
    }
}
思念绕指尖 2024-12-13 08:52:54

实际上,您不需要 __call,您可以创建自己的并受保护:

class Parent
{
    public function process($action)
    {
        // i.e. processCreateMyEntity
        $this->entityCall('process' . $action);
    }
}

class Child extends Parent
{
    protected function entityCall($methodName, $args)
    {
        $entityName = $this->parseEntityNameFromMethodCalled($methodName);
        // some actions common for a lot of entities
    }
}

根据您问题中的描述,这应该合适,但我并不完全适合当然。

Actually, you don't need __call, you can create your own and protected:

class Parent
{
    public function process($action)
    {
        // i.e. processCreateMyEntity
        $this->entityCall('process' . $action);
    }
}

class Child extends Parent
{
    protected function entityCall($methodName, $args)
    {
        $entityName = $this->parseEntityNameFromMethodCalled($methodName);
        // some actions common for a lot of entities
    }
}

According to the description in your question, this should be fitting, but I'm not entirely sure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文