php new self 详解(转)
2013-12-31 15:16
423 查看
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 co
de, for this example -- you'll have to add it back to get the singleton behavior)* :
[/code]
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 :
[/code]
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 :
[/code]
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.
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 co
de, for this example -- you'll have to add it back to get the singleton behavior)* :
[code] class MyParentClass {}
class MyChildClass extends MyParentClass {
public static function getInstance() { return new self();}
}
$a = MyChildClass::getInstance();
var_dump($a);
[/code]
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 :
[code] class MyParentClass {
public static function getInstance() { return new self();}
}
class MyChildClass extends MyParentClass {}
$a = MyChildClass::getInstance();
var_dump($a);
[/code]
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 :
[code] class MyParentClass {
public static function getInstance() { return new static();}
}
class MyChildClass extends MyParentClass {}
$a = MyChildClass::getInstance();
var_dump($a);
[/code]
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数据采集的详解
- IIS8 使用FastCGI配置PHP环境支持 过程详解
- (PHP)微信公众平台模拟登陆和发送消息详解
- lnmp安装多版本PHP共存的方法详解
- 用PHP和MYSQL建立计数器过程详解
- PHP mcrypt启用、加密以及解密过程详解
- PHP面向对象程序设计高级特性详解(接口,继承,抽象类,析构,克隆等)
- PHP命名空间(Namespace)的使用详解
- CentOS6.5下redis安装部署配置指南、常用命令、主从同步集群、redis-php学习资料整合详解
- PHP $_FILES详解
- php定时执行任务实现方法详解
- PHP使用CURL详解
- php中0,null,empty,空,false,字符串关系详解
- 详解PHP序列化反序列化的方法
- 详解cookie验证的php应用的一种SSO解决办法
- PHP exif扩展方法开启详解
- PHP cookie session 及 ThinkPHP session 详解
- php pack && unpack函数详解