Home >Backend Development >PHP Tutorial >Obtain the name of the subclass method that calls the parent class method from the parent class method?

Obtain the name of the subclass method that calls the parent class method from the parent class method?

WBOY
WBOYOriginal
2016-08-04 09:21:581242browse

<code>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;   //这里应该是XML </code>

Description: For example, instantiating $b calls its own test method. At this time, the initialization method son_function in the parent class saves the name of the method called in $b. How should this be implemented? What is this method called?

——————————————Separating line————————————————
Let me add my usage scenario: I want to record logged in users The operation and data of each step, such as when he uses the delete method, then saves the name of the controller and the method, as well as the post and get data he received. Let’s not talk about the significance of implementing this function for now. My idea is this. Each controller inherits a public controller and implements this operation in the initialization method in the public controller. Similar to the picture below, I have written pseudo code:
Obtain the name of the subclass method that calls the parent class method from the parent class method?

Reply content:

<code>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;   //这里应该是XML </code>

Description: For example, instantiating $b calls its own test method. At this time, the initialization method son_function in the parent class saves the name of the method called in $b. How should this be implemented? What is this method called?

——————————————Separating line————————————————
Let me add my usage scenario: I want to record logged in users The operation and data of each step, such as when he uses the delete method, then saves the name of the controller and the method, as well as the post and get data he received. Let’s not talk about the significance of implementing this function for now. My idea is this. Each controller inherits a public controller and implements this operation in the initialization method in the public controller. Similar to the picture below, I have written pseudo code:
Obtain the name of the subclass method that calls the parent class method from the parent class method?

  1. This goes against the object-oriented design philosophy. The parent class should not care about the implementation of the subclass

  2. If you think clearly and really want to do this, reflection is a feasible solution (but inefficient)

  3. Intuitively, you may need to use the adapter pattern

Adapter pattern:
https://en.wikipedia.org/wiki/Adapter_pattern
Obtain the name of the subclass method that calls the parent class method from the parent class method?

You may need to refine your needs more to give you a more suitable implementation solution

You can search for php late static binding, it should solve your problem

You got the logic wrong. How can you execute test or xml without instantiating the subclass? The instantiation has been completed, does the parameter change in the constructor still have any significance?

<code>$b = new B();
$b->test();
$a = new A(); // <- 此时父类实例化根本不会受到子类影响,所以你的思路本身有问题</code>

No more public function test() {$this->sonFunction = 'test';} will do

You can try using PHP’s overloaded __call() or __callstatic

http://php.net/manual/zh/language.oop5.overloading.php#object.callstatic

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn