How is the subclass method called in the framework?

Source: Internet
Author: User
Keywords php yii2 laravel php Frame
Many frameworks provide pre-post post-operation, such as the __init () method, which is called first after __construct ().

There will be Beforefind (), Afterfind () in the YII2.

These are all custom methods, such as UserModel , but UserModel extends Model .

I find that the invocation of these methods is called in the model of the parent class, why can the parent class call the methods of the subclass?

Or is it called by another method?

Add:

The __init (), Beforefind (), Afterfind (), and so forth in the framework are how these predecessor methods are called.

Reply content:

Many frameworks provide pre-post post-operation, such as the __init () method, which is called first after __construct ().

There will be Beforefind (), Afterfind () in the YII2.

These are all custom methods, such as UserModel , but UserModel extends Model .

I find that the invocation of these methods is called in the model of the parent class, why can the parent class call the methods of the subclass?

Or is it called by another method?

Add:

The __init (), Beforefind (), Afterfind (), and so forth in the framework are how these predecessor methods are called.

Late static binding

Don't know if it's something you need for static methods.

PHP: Late static binding

I don't really understand what you mean, I think the parent class does not know what the subclass method will be when it is defined, and how it might call a method of the subclass.
Are you saying that the custom method is to override the method of the parent class or completely belong to the subclass?
Good Tangled o.o
Put the code on it.

I see what you mean.
I did not install YII2, I looked for the same function as the TP, such as the controller initialization method _initialize. In fact, when instantiating the controller class, there is a call in the __construct

/**     * 架构函数 取得模板对象实例     * @access public     */    public function __construct() {        Hook::listen('action_begin',$this->config);        //实例化视图类        $this->view     = Think::instance('Think\View');        //控制器初始化        if(method_exists($this,'_initialize'))            $this->_initialize();    }

This is the code in the base class of the controller of TP, call the _initialize probably you mean to call the subclass of this initialization method.
Subclasses can inherit the methods and member variables that are modified for public.
When you instantiate a subclass, the call is definitely a subclass of the overridden method, and if there is no override, the method that inherits from the parent class is called. This is not called by the parent class, but by the child class .
You can test it under.

class MyParent{    function __construct(){        if(method_exists($this, 'test')){            $this->test();        }    }    function test(){        echo "this is parent's test\n";    }}class MyChild extends MyParent{    function test(){        echo "this is child's test\n";    }}$object1 = new MyChild();$object2 = new MyParent();

Personal understanding of Ha. o.o

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