__construct() 构造函数

1.构造函数实例化类的时候会自动调用,

2.子类没有构造函数,会直接调用父类的构造涵数, 继承父类的属性和方法

3.子类和父类都有构造函数,实例子类时不会自动调用父类构造函数,只会调用子类自己的构造函数。

用 parent::__construct();可以调用父类的构造函数。

ThinkPHP中的__initialize()和类的构造函数__construct()
网上有很多关于__initialize()的说法和用法,总感觉不对头,所以自己测试了一下。将结果和大家分享。不对请更正。
首先,我要说的是
1、__initialize()不是php类中的函数,php类的构造函数只有__construct().
2、类的初始化:子类如果有自己的构造函数(__construct()),则调用自己的进行初始化,如果没有,则调用父类的构造函数进行自己的初始化。
3、当子类和父类都有__construct()函数的时候,如果要在初始化子类的时候同时调用父类的__constrcut(),则可以在子类中使用parent::__construct().
如果我们写两个类,如下

  1. class Action{
  2. public function __construct()
  3. {
  4. echo ‘hello Action‘;
  5. }
  6. }
  7. class IndexAction extends Action{
  8. public function __construct()
  9. {
  10. echo ‘hello IndexAction‘;
  11. }
  12. }
  13. $test = new IndexAction;
  14. //output --- hello IndexAction

很明显初始化子类IndexAction的时候会调用自己的构造器,所以输出是‘hello IndexAction‘。
但是将子类修改为

  1. class IndexAction extends Action{
  2. public function __initialize()
  3. {
  4. echo ‘hello IndexAction‘;
  5. }
  6. }

那么输出的是‘hello Action‘。因为子类IndexAction没有自己的构造器。
如果我想在初始化子类的时候,同时调用父类的构造器呢?

  1. class IndexAction extends Action{
  2. public function __construct()
  3. {
  4. parent::__construct();
  5. echo ‘hello IndexAction‘;
  6. }
  7. }

这样就可以将两句话同时输出。
当然还有一种办法就是在父类中调用子类的方法。

  1. class Action{
  2. public function __construct()
  3. {
  4. if(method_exists($this,‘hello‘))
  5. {
  6. $this -> hello();
  7. }
  8. echo ‘hello Action‘;
  9. }
  10. }
  11. class IndexAction extends Action{
  12. public function hello()
  13. {
  14. echo ‘hello IndexAction‘;
  15. }
  16. }

这样也可以将两句话同时输出。
而,这里子类中的方法hello()就类似于ThinkPHP中__initialize()。
所以,ThinkPHP中的__initialize()的出现只是方便程序员在写子类的时候避免频繁的使用parent::__construct(),同时正确的调用框架内父类的构造器,所以,我们在ThnikPHP中初始化子类的时候要用__initialize(),而不用__construct(),当然你也可以通过修改框架将__initialize()函数修改为你喜欢的函数名。

转:http://www.thinkphp.cn/code/367.html

原文地址:https://www.cnblogs.com/ygyy/p/11563356.html

时间: 2024-08-01 12:15:17

__construct() 构造函数的相关文章

解决thinkPHP构造函数__construct导致tp方法冲突问题

我们在使用了__construct构造函数,覆盖了父类的构造函数,导致父类tp的方法无法使用,例如$this->display(),解决办法是: 在__construct函数中调用一下父类的构造函数. 1 function __construct(){ 2 parent::__construct(); 3 }

_initialize() 区别 __construct()

_initialize()方法是在任何方法执行之前,都要执行的,当然也包括 __construct构造函数. 也就是说如果存在_initialize()函数,调用对象的任何方法都会导致_initialize()函数的自动调用,而__construct()构造函数仅仅在创建对象的时候调用一次,跟其它方法调用没有关系. __construct这里是双划线,而_initialize()函数是单划线 如果父子类均有_initialize()函数,则子类覆盖了父类的,如果子类没有而父类有,则子类继承父类的

PHP中的抽象类与抽象方法/静态属性和静态方法/PHP中的单利模式(单态模式)/串行化与反串行化(序列化与反序列化)/约束类型/魔术方法小结

  前  言  OOP  学习了好久的PHP,今天来总结一下PHP中的抽象类与抽象方法/静态属性和静态方法/PHP中的单利模式(单态模式)/串行化与反串行化(序列化与反序列化). 1  PHP中的抽象类与抽象方法 1.什么是抽象方法?              没有方法体 {} 的方法,必须使用abstract 关键字修饰.这样的方,我们叫做抽象方法.                    abstract function say(); //    抽象方法 2.什么是抽象类?        

php基础---单例模式&&工厂模式

//PHP设计模式 //工厂模式:工厂类 用于创建对象 interface Dongwu { function say(); } class Mao implements Dongwu { public function say() { echo "喵喵!我是一只小野猫!"; } } //工厂类,用来创建猫类 class GongChang { public static function GetObj($className) { return new $className(); }

PHP中的面向对象OOP中的魔术方法

一.什么是魔术方法: PHP为我们提供了一系列用__开头的函数,这些函数无需自己手动调用,会在合适的时机自动调用,这类函数称为魔术函数.例如: function __construct(){} 在new一个新对象时自动调用此函数 二.PHP中都有那些魔术方法,以及它们的作用:1.__construct():构造函数,new对象时自动调用 eg: class Person{ public $name; public $age; function __construct($name,$age){ $

redis中文文档

phpredis是php的一个扩展,效率是相当高有链表排序功能,对创建内存级的模块业务关系 很有用;以下是redis官方提供的命令使用技巧: 下载地址如下: https://github.com/owlient/phpredis(支持redis 2.0.4) Redis::__construct构造函数$redis = new Redis(); connect, open 链接redis服务参数host: string,服务地址port: int,端口号timeout: float,链接时长 (

php PDO mysql写法

php PDO写法连接mysql: 写法一: $db="mysql:host=localhost;dbname=sql" : //连接数据,地址localhost:数据库名称sql: $username="root"; //数据库登录账号: $password="root"; //数据库登录密码: try{ $pdo=new PDO($db,$username,$password);   //连接数据库赋值$pdo; }catch(PDOExce

redis 操作大全 PHP-redis中文文档

转自  : http://www.cnblogs.com/weafer/archive/2011/09/21/2184059.html phpredis是php的一个扩展,效率是相当高有链表排序功能,对创建内存级的模块业务关系 很有用;以下是redis官方提供的命令使用技巧: 下载地址如下: https://github.com/owlient/phpredis(支持redis 2.0.4) Redis::__construct构造函数$redis = new Redis(); connect,

PHP-redis api 中文说明(转)

来源 : http://hi.baidu.com/gaolamp/item/1686aac07334bd0f0ad93a9f PHP-redis api 中文说明 phpredis 是 php 的一个扩展,效率是相当高有链表排序功能,对创建内存级的模块业务 关系,很有用,以下是 redis 官方提供的命令使用技巧: 下载地址如下: https://github.com/owlient/phpredis(支持 redis 2.0.4) Redis::__construct 构造函数 $redis