dongpanbo4727
2017-09-20 13:17
浏览 21
已采纳

PHP OOP类和函数

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. As you can see, in A::aa() AA::bb() is called.

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. I can only change the B class or the C class.

My idea was to add a listener in the B class and overwrite the bb() function like this

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.

PHP version is 5.3

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题