zoukankan      html  css  js  c++  java
  • __construct()和__initialize()

    当然还有一种办法就是在父类中调用子类的方法。
    class Action{
        public function __construct()
        {
            if(method_exists($this,'hello'))
            {
                $this -> hello();
            }
            echo 'hello Action';
        }
    }
    class IndexAction extends Action{
        public function hello()
        {
            echo 'hello IndexAction';
        }
    }
    复制代码
    这样也可以将两句话同时输出。
    而,这里子类中的方法hello()就类似于ThinkPHP中__initialize()。
    所以,ThinkPHP中的__initialize()的出现只是方便程序员在写子类的时候避免频繁的使用parent::__construct(),同时正确的调用框架内父类的构造器,所以,我们在ThnikPHP中初始化子类的时候要用__initialize(),而不用__construct(),当然你也可以通过修改框架将__initialize()函数修改为你喜欢的函数名。
  • 相关阅读:
    继承与多态
    本周总结
    总结
    周总结
    周总结
    第三周总结
    .......
    .....
    ....
    ....
  • 原文地址:https://www.cnblogs.com/suxiaolong/p/5660732.html
Copyright © 2011-2022 走看看