zoukankan      html  css  js  c++  java
  • PHP构造函数的执行顺序

    PHP构造函数的执行顺序

    测试代码如下:
    <?php
    class grandfather {
        public function __construct(){
            echo 'grandfather';
        }
    }
    class father extends grandfather {
        public function __construct(){
            echo 'father';
        }
    }
    class son extends father {
        public function __construct(){
            echo 'son';
        }
    }
    $test = new son();
    结果是 :   son
    <?php
    class grandfather {
        public function __construct(){
            echo 'grandfather';
        }
    }
    class father extends grandfather {
        public function __construct(){
            echo 'father';
        }
    }
    class son extends father {
        
    }
    $test = new son();
    结果: father
    <?php
    class grandfather {
        public function __construct(){
            echo 'grandfather';
        }
    }
    class father extends grandfather {
    
    }
    class son extends father {
        
    }
    $test = new son();
    结果:grandfather
    
    由此可见php类的构造函数调用是 从自身向上查找,执行最近的一个,然后stop
  • 相关阅读:
    104每日博客
    924每日博客
    921每日博客
    928每日博客
    929每日博客
    930每日博客
    927每日博客
    大志非才不就,大才非学不成—我的博文资源汇总
    vue 项目搭建
    Vant 使用记录
  • 原文地址:https://www.cnblogs.com/hongfei/p/2687511.html
Copyright © 2011-2022 走看看