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)* :

   1: class MyParentClass { }

   2:  

   3: class MyChildClass extends MyParentClass {

   4:  

   5:      public static function getInstance() { return new self(); }

   6:  

   7: }

   8:  

   9: $a = MyChildClass::getInstance();

  10:  

  11:  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 :

   1: class MyParentClass {

   2:  

   3:     public static function getInstance() { return new self(); }

   4:  

   5:  }

   6:  

   7: class MyChildClass extends MyParentClass { }

   8:  

   9: $a = MyChildClass::getInstance();

  10:  

  11: var_dump($a);

  12:  

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 :

   1: class MyParentClass {

   2:  

   3:     public static function getInstance() { return new static(); }

   4:  

   5: }

   6:  

   7: class MyChildClass extends MyParentClass { }

   8:  

   9: $a = MyChildClass::getInstance();

  10:  

  11: 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 详解(转)的更多相关文章

  1. Linq之旅:Linq入门详解(Linq to Objects)

    示例代码下载:Linq之旅:Linq入门详解(Linq to Objects) 本博文详细介绍 .NET 3.5 中引入的重要功能:Language Integrated Query(LINQ,语言集 ...

  2. 架构设计:远程调用服务架构设计及zookeeper技术详解(下篇)

    一.下篇开头的废话 终于开写下篇了,这也是我写远程调用框架的第三篇文章,前两篇都被博客园作为[编辑推荐]的文章,很兴奋哦,嘿嘿~~~~,本人是个很臭美的人,一定得要截图为证: 今天是2014年的第一天 ...

  3. EntityFramework Core 1.1 Add、Attach、Update、Remove方法如何高效使用详解

    前言 我比较喜欢安静,大概和我喜欢研究和琢磨技术原因相关吧,刚好到了元旦节,这几天可以好好学习下EF Core,同时在项目当中用到EF Core,借此机会给予比较深入的理解,这里我们只讲解和EF 6. ...

  4. Java 字符串格式化详解

    Java 字符串格式化详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 文中如有纰漏,欢迎大家留言指出. 在 Java 的 String 类中,可以使用 format() 方法 ...

  5. Android Notification 详解(一)——基本操作

    Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...

  6. Android Notification 详解——基本操作

    Android Notification 详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 前几天项目中有用到 Android 通知相关的内容,索性把 Android Notificatio ...

  7. Git初探--笔记整理和Git命令详解

    几个重要的概念 首先先明确几个概念: WorkPlace : 工作区 Index: 暂存区 Repository: 本地仓库/版本库 Remote: 远程仓库 当在Remote(如Github)上面c ...

  8. Drawable实战解析:Android XML shape 标签使用详解(apk瘦身,减少内存好帮手)

    Android XML shape 标签使用详解   一个android开发者肯定懂得使用 xml 定义一个 Drawable,比如定义一个 rect 或者 circle 作为一个 View 的背景. ...

  9. Node.js npm 详解

    一.npm简介 安装npm请阅读我之前的文章Hello Node中npm安装那一部分,不过只介绍了linux平台,如果是其它平台,有前辈写了更加详细的介绍. npm的全称:Node Package M ...

  10. .NET应用和AEAI CAS集成详解

    1 概述 数通畅联某综合SOA集成项目的统一身份认证工作,需要第三方系统配合进行单点登录的配置改造,在项目中有需要进行单点登录配置的.NET应用系统,本文专门记录.NET应用和AEAI CAS的集成过 ...

随机推荐

  1. Ubuntu 网管服务器配置

    1.设置Linux内核支持ip数据包的转发 echo "1" > /proc/sys/net/ipv4/ip_forward or vi /etc/sysctl.conf   ...

  2. PHP之音乐ID3扩展

    不知道你有没有这样的经历,一些从网上下载的MP3音乐,文件名明明是01.02这样的序号,但播放时却能显示出歌曲的正确名称来,是不是有点奇妙? 这其实都是ID3在暗中相助 ID3,一般是位于一个mp3文 ...

  3. linux expr命令参数及用法详解

    expr用法 expr命令一般用于整数值,但也可用于字符串.一般格式为: #expr argument operator argument expr也是一个手工命令行计数器. #$expr 10 + ...

  4. 访问WEB-INF下的jsp/html

    基于Servlet的声明,WEB-INF不作为Web应用的公共文档树的一部分.因此,WEB-INF 目录下的资源不是为客户直接服务的.因此客户端不能直接访问WEB-INF下的文件,但是可以通过requ ...

  5. TFS 创建分支

    1 2 3 4

  6. 【转】从1到N这N个数中1的出现了多少次?

    给定一个十进制整数N,求出从1到N的所有整数中出现"1"的个数. 例如:N=2,1,2出现了1个"1". N=12,1,2,3,4,5,6,7,8,9,10,1 ...

  7. XMEAG-128A1

    JTAGUSERID = 0x00 WDWP = 8CLK WDP = 8CLK DVSDON = [ ] BOOTRST = BOOTLDR BODPD = DISABLED RSTDISBL = ...

  8. Objective-C相关Category的收集

    Objective-C相关Category的收集 Categories是给你得不到源码的classes增加功能的一种方法.这个页面收集一些相关的Category,并且持续更新,你可以订阅关注.作者是F ...

  9. math ceil以及math floor,math:round

    ◎Math.ceil()执行向上舍入,即它总是将数值向上舍入为最接近的整数:◎Math.floor()执行向下舍入,即它总是将数值向下舍入为最接近的整数:◎Math.round()执行标准舍入,即它总 ...

  10. 河南多校大一训练赛 E 开餐馆

    题目链接:http://acm.hust.edu.cn/vjudge/contest/125004#problem/E 密码:acm Description 北大信息学院的同学小明毕业之后打算创业开餐 ...