
protected:只能自己玩,或者找自己的父亲或者儿子玩。
private:只能自己玩
public:公交车
__construct(构造方法):类中优先调用此方法,如果有父类,父类和子类也有此方法的时候。new子类,得到的结果是会调用其自己的__construct方法,而不是用父类的。在TP中有个方法叫_initialize方法。在TP中,我感觉他们是把这个函数当作了__construct的替代品,但是_initialize优先级小于__construct。
当调用子类的时候要想子类和父类都输出
<?php class Action{ public function __construct() { echo 'hello Action'; } } class IndexAction extends Action{ public function __construct() { parent::__construct(); echo "<br />"; echo 'hello IndexAction'; } } $test = new IndexAction; ?>
TP中
<?php class Action{ public function __construct() { if(method_exists($this,'_initialize')) { $this -> _initialize(); } echo "<br />"; echo 'hello Action'; } } class IndexAction extends Action{ public function _initialize() { echo 'hello IndexAction'; } } $test = new IndexAction; ?>当我们new子类的时候,会优先调用其父类的__construct,判断其本类和子类是否有_initialize方法,如果有就调用,输出,然后再输出自己的数据。所以,TP中。我们子类建议用_initialize方法,不建议用parent方法。
文章来源: 关于PHP函数protected,private,public 和 __construct方法
人吐槽 | 人点赞 |
发表评论