__construct()和__initialize()
来源:互联网 发布:java 获取jsp页面内容 编辑:程序博客网 时间:2024/04/29 13:12
首先,我就先说说php中的继承,__construct是类中的构造函数,用于实例化
1 <?php 2 class Action{ 3 public function __construct(){ 4 echo 'Action'; 5 } 6 } 7 8 class SonAction extends Action{ 9 public function __construct(){10 echo 'Son Action';11 }12 }13 14 $son = new SonAction();15 16 //result:Son Action17 ?>
在父类中定义构造函数,子类中使用构造函数,示例化子类,输出Son Action,没有调用父类中的构造函数
<?php class Action{ public function __construct(){ echo 'Action'; } } class SonAction extends Action{ public function __construct(){ parent::__construct(); echo 'Son Action'; } } $son = new SonAction(); //result:ActionSon Action?>
在子类中,使用parent::__construct(),调用父类的构造函数,因为继承了父类,定义自己的构造方法,重载了__construct(),所以需要调用父类的构造函数,需要parent::__construct
<?php class Action{ public function __construct(){ echo 'Action'; } } class SonAction extends Action{ } $son = new SonAction(); //result:Action?>
在继承的子类中没有自己的构造函数,可以说是继承了父类的构造函数(个人理解,可以探讨一下),所以实例化的时候,输出得到result
//接下来我说说_initialize 这个函数
其实这个函数不是原生php含有的,而是一个TP自己定义的一个函数
我们可以再TP的框架上看到,我的版本是3.1.3
/** * 架构函数 取得模板对象实例 * @access public */ public function __construct() { tag('action_begin',$this->config); //实例化视图类 $this->view = Think::instance('View'); //控制器初始化 if(method_exists($this,'_initialize')) $this->_initialize(); }
这个函数是Action中的构造函数,在控制器初始化中,使用了method_exists,判断当前实例化的类中是否函数_initialize这个函数,
当子类没有构造函数的时候,子类继承父类的构造函数(子类会继承[或调用最近父类的构造函数]),判断子类中是否含有_initialize这个函数,
若含有,调用函数,在构造的时候调用该函数,在construct之后。。。。。
下面这个例子:可以仔细观察出来
Action类的构造器 public function __construct() { echo 'grand father'; tag('action_begin',$this->config); //实例化视图类 $this->view = Think::instance('View'); //控制器初始化 if(method_exists($this,'_initialize')) $this->_initialize(); } TestAction类的构造器 class TestAction extends Action{ public function __construct(){ echo 'Test override Action'; } } SonatestAction类初始化函数 class SontestAction extends TestAction{ public function _initialize(){ echo 'Son test'; } public function index(){ echo 222; die(); } } //输出得到Test override Action222
上面的例子得到结果分析可得到: TestAction重载了Action中的构造器,所以访问SontestAction中的index的时候,首先会调用构造函数,则会调用父类的构造器,父类构造器中不含有调用_initialize所以。。。子类中没有调用_initialize
转自:http://www.cnblogs.com/zafuacm/p/4204198.html
- __construct()和__initialize()
- __construct()和__initialize()
- ThinkPHP之__construct()和__initialize()
- ThinkPHP之__construct()和__initialize()
- ThinkPHP之__construct()和__initialize()
- ThinkPHP之__construct()和__initialize()
- tp框架__construct()和__initialize()
- ThinkPHP之__construct()和__initialize()
- ThinkPHP之__construct()和__initialize()
- ThinkPHP之__construct()和__initialize()
- thinkphp中__initialize和__construct的区别
- thinkphp篇之__construct()和__initialize()
- __initialize()和类的构造函数__construct()
- ThinkPHP中的__initialize()和类的构造函数__construct()
- __construct()和__initialize(),initialize只是为了少写parent::construct()
- ThinkPHP中__initialize()和类的构造函数__construct()用法分析
- thinkphp 中_initialize和__construct的区别
- PHP构造函数__construct和析构函数__destruct
- WMWARE下/sbin/scsi_id 返回值为空
- RAC执行root.sh报libcap.so.1: cannot open shared object file
- 遇到关于MySQL权限的两个问题
- Failed to register Grid Infrastructure type ora.mdns.type
- multipathd dead but pid file exists
- __construct()和__initialize()
- CRS-2800 CRS-4000
- Oracle 11gR2 RAC ohasd failed to start 解决方法
- JS原生获取class对象(getElementsByClassName)
- RAC时间同步的两种方法
- RAC 开启gsd和oc4j服务
- Java设计模式之工厂设计模式
- CentOS6+MySQL5.6二进制安装
- MySQL忘记密码怎么办