Gets the subclass method name in the parent class method that called the parent class method?

Source: Internet
Author: User
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:

    1. This violates the object-oriented design philosophy, and the parent class should not care about the implementation of the subclass.

    2. If you think about it, it's really going to do that. Reflection is a viable option (but inefficient)

    3. 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

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.