简体   繁体   English

PHP OOP类和函数

[英]PHP OOP classes and functions

I am not sure how to name this, but here it goes. 我不确定如何命名,但是就此开始。 Lets suppose i have the following 假设我有以下内容

class A {
    public function aa() {
       $this->bb();
    }
    public function bb() {

    }
}

class B extends a {

}

class C {
   __construct(B $service) {
       $this->service = $service;
   }
   public function aa() {

       $this->service->aa();
   }
}

My call in code will be 我的代码调用将是

$C = new C(new B());
$C->aa();

So this will basically execute A:aa() which is what i want. 因此,这基本上将执行我想要的A:aa() As you can see, in A::aa() AA::bb() is called. 如您所见,在A::aa()中将调用AA::bb()

What I need. 我需要的。 When AA::bb() is called i want to execute some code defined in class C, but I am not allowed to change the A class. 调用AA :: bb()时,我想执行C类中定义的某些代码,但不允许更改A类。 I can only change the B class or the C class. 我只能更改B类或C类。

My idea was to add a listener in the B class and overwrite the bb() function like this 我的想法是在B类中添加一个侦听器并像这样覆盖bb()函数

class B extends a {
    public $listener;
    bb() {
       parent::bb();
       $this->listener();
    }
}

class C {
   __construct(B $service) {
       $this->service = $service;
   }
   public function aa() {

       $this->service->listener = function() { }
       $this->service->aa();
   }
}

But I don't like this idea a lot, doesn't look like a good one. 但是我不太喜欢这个主意,看起来也不是一个好主意。 What are my options here? 我在这里有什么选择?

Again, I CANNOT change the A class and i can only call the C class. 同样,我不能更改A类,而我只能调用C类。

PHP version is 5.3 PHP版本是5.3

You have two options. 您有两个选择。 Extend or decorate. 延伸或装饰。

First one would be kinda what you have already written, though, I would not use public visibility for the listener: 第一个有点像您已经编写的内容,但是,我不会对侦听器使用public可见性:

class Foo extends A {
    private $listener;
    public function setListener(callable $func) {
        $this->listener = $func;
    }
    public function bb() {
        call_user_func($this->listener);
        return parent:bb();
    }
}

In the example I passed the listener via setter injection, but you can also use constructor injection and pass the $listened in the overloaded __construct() method. 在该示例中,我通过setter注入传递了侦听器,但是您也可以使用构造函数注入,并在重载的__construct()方法中传递$listened __construct() When you extend a class, the "interface restriction" does not aply to the constructor's signature. 扩展类时,“接口限制”不适用于构造函数的签名。


The other approach is to use a decorator: 另一种方法是使用装饰器:

class Foo {
    private $target;
    public function __construct(A $target) {
        $this->target = $target;
    }

    public function bb($callback) {
        $callback();
        return $this->target->bb();
    }

    public function __call($method, $arguments) {
        return call_user_func_array( 
                array( $this->target, $method ),
                $arguments
            );
    }
}

The second approach would let you alter the interface. 第二种方法是让您更改界面。

Which option you pick depend on the exact functionality you actually need to implement. 您选择哪个选项取决于您实际需要实现的确切功能。 The decorator is a solution for, when you need drastic change in the objects behavior - for example, it is really good for adding access control. 装饰器是一种解决方案,用于当您需要对对象行为进行彻底更改时-例如,对于添加访问控制而言,它真的很好。

I understand that you want to execute code in C after code in A completes. 我了解您要在A代码完成后在C执行代码。 You cannot change A. 您不能更改A。

As written, C::aa calls A::aa , which calls A::bb and the stack unwinds. 如所写, C::aa调用A::aa ,后者调用A::bb并展开堆栈。 Why not just do the work in C::aa after the service call finishes? 为什么在服务调用结束后不仅仅在C::aa工作?

class C {
   public function aa() {
       $this->service->aa();
       // whatever you want to do
   }
}

If, on the other hand, you need to call code after A::aa is called but before A::bb is called then the example you posted would suffice with clarity: 另一方面,如果您需要在调用A::aa之后但调用A::bb 之前调用代码,那么您发布的示例就足够清楚了:

class B extends a {
    public $listener;
    public function bb() {
       call_user_func($this->listener);
       parent::bb();
    }
}

Note the use of call_user_func , which is necessary for PHP 5.3 to call an anonymous function stored in a member variable. 注意使用call_user_func ,这是PHP 5.3调用存储在成员变量中的匿名函数所必需的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM