PHP中new static()与new self()的区别异同分析


Posted in PHP onAugust 22, 2014

本文实例讲述了PHP中new static()与new self()的区别异同,相信对于大家学习PHP程序设计能够带来一定的帮助。

问题的起因是本地搭建一个站。发现用PHP 5.2 搭建不起来,站PHP代码里面有很多5.3以上的部分,要求更改在5.2下能运行。

改着改着发现了一个地方

return new static($val);

这尼玛是神马,只见过

return new self($val);

于是上网查了下,他们两个的区别。

self - 就是这个类,是代码段里面的这个类。

static - PHP 5.3加进来的只得是当前这个类,有点像$this的意思,从堆内存中提取出来,访问的是当前实例化的那个类,那么 static 代表的就是那个类。

还是看看老外的专业解释吧:

self refers to the same class whose method the new operation takes place in.

static in PHP 5.3's late static bindings refers to whatever class in the hierarchy which you call the method on.

In the following example, B inherits both methods from A. self is bound to A because it's defined in A's implementation of the first method, whereas static is bound to the called class (also see  get_called_class() ).

class A {
  public static function get_self() {
    return new self();
  }

  public static function get_static() {
    return new static();
  }
}

class B extends A {}

echo get_class(B::get_self()); // A
echo get_class(B::get_static()); // B
echo get_class(A::get_static()); // A

这个例子基本上一看就懂了吧。

原理了解了,但是问题还没有解决,如何解决掉 return new static($val); 这个问题呢?

其实也简单就是用 get_class($this); 代码如下:

class A {
  public function create1() {
    $class = get_class($this);
return new $class();
  }
  public function create2() {
    return new static();
  }
}

class B extends A {

}

$b = new B();
var_dump(get_class($b->create1()), get_class($b->create2()));

/*
The result 
string(1) "B"
string(1) "B"
*/

感兴趣的朋友可以动手测试一下示例代码,相信会有新的收获!

PHP 相关文章推荐
两个强悍的php 图像处理类1
Jun 15 PHP
php学习笔记 php中面向对象三大特性之一[封装性]的应用
Jun 13 PHP
php 目录与文件处理-郑阿奇(续)
Jul 04 PHP
PHP源码之explode使用说明
Aug 05 PHP
基于PHP array数组的教程详解
Jun 05 PHP
PHP中将ip地址转成十进制数的两种实用方法
Aug 15 PHP
PHP实现Javascript中的escape及unescape函数代码分享
Feb 10 PHP
php使用GD创建保持宽高比缩略图的方法
Apr 17 PHP
php生成gif动画的方法
Nov 05 PHP
PHP模拟http请求的方法详解
Nov 09 PHP
php面向对象程序设计入门教程
Jun 22 PHP
PHP中的替代语法简介
Aug 22 #PHP
destoon供应信息title调用出公司名称的方法
Aug 22 #PHP
destoon实现不同会员组公司名称显示不同的颜色的方法
Aug 22 #PHP
destoon文章模块调用企业会员资料的方法
Aug 22 #PHP
destoon实现会员商铺中指定会员或会员组投放广告的方法
Aug 21 #PHP
destoon实现调用图文新闻的方法
Aug 21 #PHP
destoon切换城市后实现logo旁边显示地区名称的方法
Aug 21 #PHP
You might like
自己在做项目过程中学到的PHP知识收集
2012/08/20 PHP
解析Linux下Varnish缓存的配置优化
2013/06/20 PHP
php采集文章中的图片获取替换到本地(实现代码)
2013/07/08 PHP
destoon安全设置中需要设置可写权限的目录及文件
2014/06/21 PHP
jQuery帮助之CSS尺寸(五)outerHeight、outerWidth
2009/11/14 Javascript
js 效率组装字符串 StringBuffer
2009/12/23 Javascript
jquery 提交值不为空的元素示例代码
2013/05/10 Javascript
使用js实现关闭js弹出层的窗口
2014/02/10 Javascript
详解JavaScript中的事件流和事件处理程序
2016/05/20 Javascript
NodeJs的优势和适合开发的程序
2016/08/14 NodeJs
JavaScript中undefined和null的区别
2017/05/03 Javascript
解决vue.js在编写过程中出现空格不规范报错的问题
2017/09/20 Javascript
JavaScript实现左侧菜单效果
2017/12/14 Javascript
使用jquery的cookie实现登录页记住用户名和密码的方法
2019/03/13 jQuery
微信小程序实现授权登录
2019/05/15 Javascript
python正则表达式re模块详解
2014/06/25 Python
Python常用的文件及文件路径、目录操作方法汇总介绍
2015/05/21 Python
深入理解Python中字典的键的使用
2015/08/19 Python
浅谈tensorflow1.0 池化层(pooling)和全连接层(dense)
2018/04/27 Python
Selenium定位元素操作示例
2018/08/10 Python
django foreignkey(外键)的实现
2019/07/29 Python
Python Lambda函数使用总结详解
2019/12/11 Python
Python Numpy,mask图像的生成详解
2020/02/19 Python
Python面向对象实现方法总结
2020/08/12 Python
Python实现随机爬山算法
2021/01/29 Python
canvas实现飞机打怪兽射击小游戏的示例代码
2018/07/09 HTML / CSS
FOREO官方网站:LUNA露娜洁面仪
2016/11/28 全球购物
伊莱克斯阿根廷网上商店:Tienda Electrolux
2021/03/08 全球购物
关于毕业的广播稿
2014/01/10 职场文书
报告会主持词
2014/04/02 职场文书
镇党委书记群众路线整改措施思想汇报
2014/10/13 职场文书
撤回我也能看到!教你用Python制作微信防撤回脚本
2021/06/11 Python
Win7/8.1用户可以免费升级到Windows 11系统吗?
2021/11/21 数码科技
instantclient客户端 连接oracle数据库
2022/04/26 Oracle
IIS服务器中设置HTTP重定向访问HTTPS
2022/04/29 Servers
Python序列化模块JSON与Pickle
2022/06/05 Python