php new self 详解
来源:互联网 发布:针式打印机模板软件 编辑:程序博客网 时间:2024/06/06 09:18
self points to the class in which it is written. So, if your getInstance method is in a class name MyClass, the following line : self::$_instance = new self(); Will do the same as : self::$_instance = new MyClass(); Edit : a couple more informations, after the comments. If you have two classes that extend each other, you have two situations : getInstance is defined in the child class getInstance is defined in the parent class The first situation would look like this (I've removed all non-necessary code, for this example -- you'll have to add it back to get the singleton behavior)* :
class MyParentClass { }
class MyChildClass extends MyParentClass {
public static function getInstance() { return new self(); }
}
$a = MyChildClass::getInstance();
var_dump($a);
Here, you'll get : object(MyChildClass)#1 (0) { } Which means self means MyChildClass -- i.e. the class in which it is written.
For the second situation, the code would look like this :
class MyParentClass {
public static function getInstance() { return new self(); }
}
class MyChildClass extends MyParentClass { }
$a = MyChildClass::getInstance();
var_dump($a);
And you'd get this kind of output : object(MyParentClass)#1 (0) { } Which means self means MyParentClass -- i.e. here too, the class in which it is written.
With PHP < 5.3, that "the class in which it is written" is important -- and can sometimes cause problems. That's why PHP 5.3 introduces a new usage for the static keyword : it can now be used exactly where we used self in those examples :
class MyParentClass {
public static function getInstance() { return new static(); }
}
class MyChildClass extends MyParentClass { }
$a = MyChildClass::getInstance();
var_dump($a);
But, with static instead of self, you'll now get : object(MyChildClass)#1 (0) { } Which means that static sort of points to the class that is used (we used MyChildClass::getInstance()), and not the one in which it is written. Of course, the behavior of self has not been changed, to not break existing applications -- PHP 5.3 just added a new behavior, recycling the static keyword. And, speaking about PHP 5.3, you might want to take a look at the Late Static Bindings page of the PHP manual.
- php new self 详解
- php new self()是什么意思
- PHP中new static()与new self()
- PHP中new static() 和 new self()
- PHP中new static()与new self()的区别
- PHP中new static()与new self()的区别
- 【PHP趣味】new self和new static的区别
- PHP中new static() 和 new self() 的区别
- PHP new staic() 和 new self()的区别
- PHP中new self()和new static()的区别探究
- PHP中new self()和new static()的区别
- PHP 中 new static 和 new self 的区别
- PHP的self::与static::,new self()和new static()之分
- PHP this,self 和 parent 关键字 详解
- PHP this,self 和 parent 关键字 详解
- PHP中new static()与new self()的区别异同分析
- PHP中new static()与new self()的区别异同分析
- PHP中new static()与new self()的区别异同分析
- 解决iPhone网络软件在睡眠情况断线问题
- 怎么在Myeclipse里重新编译Java文件
- Crosstool 、 Makefile、u-boot的Makefile
- Failed to install *.apk on device 真机测试时问题 android
- smarty中使用变量
- php new self 详解
- Spring portlet 配置文件 一个问题
- PPPOE服务器如何使客户端拨上号就上网
- 用ul、li做横向导航(float:left 向左漂移)
- (7)巩固一下C语言基础知识,谁动手,谁收获
- 模板文件中分配的变量
- win7下 UAC及虚拟化的测试
- SELECT INTO 和 INSERT INTO SELECT 两种表复制语句
- eclipse 快捷键大全