class A{ public $son_function; public function __construct() { $this->son_function = ? //这里获取到子类的方法名 }}class B extends A{ public function test() { } public function XML() { }}$b = new B();$b->test();echo $b->son_function; //这里应该是test $b->XML();echo $b->son_function;
Description: For example, the instantiation of the $b
method called itself test
, this time the initialization method in the parent class saves the name of the son_function
$b
calling method, how should this be implemented? What's the name of this way?
———————————————— Split Line —————————————————
To add to my use of the scene: I want to log the user every step of the operation and data, such as he used the deletion method, and then save the controller and the name of the method, as well as he received the post and get data. not to mention the significance of implementing this function. My idea is this: Each controller inherits a common controller, which is implemented in the initialization method of the public controller. Similar to this, I have already written the pseudo-code:
Reply content:
class A{ public $son_function; public function __construct() { $this->son_function = ? //这里获取到子类的方法名 }}class B extends A{ public function test() { } public function XML() { }}$b = new B();$b->test();echo $b->son_function; //这里应该是test $b->XML();echo $b->son_function;
Description: For example, the instantiation of the $b
method called itself test
, this time the initialization method in the parent class saves the name of the son_function
$b
calling method, how should this be implemented? What's the name of this way?
———————————————— Split Line —————————————————
To add to my use of the scene: I want to log the user every step of the operation and data, such as he used the deletion method, and then save the controller and the name of the method, as well as he received the post and get data. not to mention the significance of implementing this function. My idea is this: Each controller inherits a common controller, which is implemented in the initialization method of the public controller. Similar to this, I have already written the pseudo-code:
This violates the object-oriented design philosophy, and the parent class should not care about the implementation of the subclass.
If you think about it, it's really going to do that. Reflection is a viable option (but inefficient)
Intuitively, you may need to use the adapter mode
Adapter mode:
Https://en.wikipedia.org/wiki/Adapter_pattern
You may need to refine your requirements to give you a more appropriate implementation.
You can search the late php static bindings , you should be able to solve your problem
You've got the wrong logic. How do I execute test or XML without instantiating the subclass? has already been instantiated, what is the meaning of the parameter changes in the constructor?
$b = new B();$b->test();$a = new A(); // <- 此时父类实例化根本不会受到子类影响,所以你的思路本身有问题
Besides, public function test () {$this->sonfunction = ' Test ';} No, it's okay.
You can use PHP's overloaded __call () or __callstatic to try
Http://php.net/manual/zh/language.oop5.overloading.php#object.callstatic