PHP中new static()与new self()的比较


Posted in PHP onAugust 19, 2016

今天在coding的时候,发现了 new static(),觉得实例化的地方不是应该是 new self()吗?查询了一下才知道两者的区别:

1)在有子类集成的时候,两者的表现不一样

2)php 5.2及以下的版本不支持 new static()的语法

简单通俗的来说, self就是写在哪个类里面, 实际调用的就是这个类.所谓的后期静态绑定, static代表使用的这个类, 就是你在父类里写的static,

然后通过子类直接/间接用到了这个static, 这个static指的就是这个子类, 所以说static和$this很像, 但是static可以用于静态方法和属性等.

具体解释如下:

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 Person {
public static function get_self() {
return new self();
}
public static function get_static() {
return new static();
}
}
class WangBaoqiang extends Person{}
echo get_class(WangBaoqiang::get_self()); // Person
echo get_class(WangBaoqiang::get_static()); // WangBaoqiang
echo get_class(Person::get_static()); // Person

但是如果想让 子类使用 get_class时,返回的也是 当前子类的名称('wangbaoqiang'),该怎么做呢。

<?php
class Person {
public function create1() {
$class = get_class($this);
return new $class();
}
public function create2() {
return new static();
}
}
class WangBaoqiang extends Person {
}
$wangBaoQiang = new WangBaoqiang();
var_dump(get_class($wangBaoQiang->create1()), get_class($wangBaoQiang->create2()));
/*
The result 
string(1) "WangBaoqiang"
string(1) "WangBaoqiang"
*/

以上所述是小编给大家介绍的PHP中new static()与new self()的比较,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

PHP 相关文章推荐
一篇入门的php Class 文章
Apr 04 PHP
兼容性最强的PHP生成缩略图的函数代码(修改版)
Jan 18 PHP
PHP中Date()时间日期函数的使用方法小结
Apr 20 PHP
Ping服务的php实现方法,让网站快速被收录
Feb 04 PHP
setcookie中Cannot modify header information-headers already sent by错误的解决方法详解
May 08 PHP
定义php常量的详解
Jun 09 PHP
IIS6.0中配置php服务全过程解析
Aug 07 PHP
PHP使用GETDATE获取当前日期时间作为一个关联数组的方法
Mar 19 PHP
PHP+MySQL实现无极限分类栏目的方法
Dec 23 PHP
PHPCMS V9 添加二级导航的思路详解
Oct 20 PHP
thinkPHP5实现数据库添加内容的方法
Oct 25 PHP
通过源码解析Laravel的依赖注入
Jan 22 PHP
ThinkPHP3.2.1图片验证码实现方法
Aug 19 #PHP
PHP自定义图片缩放函数实现等比例不失真缩放的方法
Aug 19 #PHP
Thinkphp框架开发移动端接口(2)
Aug 18 #PHP
Thinkphp框架开发移动端接口(1)
Aug 18 #PHP
PHP Ajax JavaScript Json获取天气信息实现代码
Aug 17 #PHP
PHP文件下载实例代码浅析
Aug 17 #PHP
PHP Ajax实现无刷新附件上传
Aug 17 #PHP
You might like
PHP针对多用户实现更换头像功能
2016/09/04 PHP
PHP 获取指定地区的天气实例代码
2017/02/08 PHP
yii2简单使用less代替css示例
2017/03/10 PHP
php实现获取农历(阴历)、节日、节气的类与用法示例
2017/11/20 PHP
php curl获取到json对象并转成数组array的方法
2018/05/31 PHP
php-7.3.6 编译安装过程
2020/02/11 PHP
jQuery 常见操作实现方式和常用函数方法总结
2011/05/06 Javascript
Javascript弹出窗口的各种方法总结
2013/11/11 Javascript
Javascript数据结构与算法之列表详解
2015/03/12 Javascript
Javascript 数组去重的方法(四种)详解及实例代码
2016/11/24 Javascript
js删除数组中的元素delete和splice的区别详解
2018/02/03 Javascript
angular实现页面打印局部功能的思考与方法
2018/04/13 Javascript
详解React native fetch遇到的坑
2018/08/30 Javascript
解决vue js IOS H5focus无法自动弹出键盘的问题
2018/08/30 Javascript
webpack4.0 入门实践教程
2018/10/08 Javascript
微信小程序动态添加和删除组件的现实
2020/02/28 Javascript
[01:12:27]EG vs Secret 2018国际邀请赛淘汰赛BO3 第二场 8.22
2018/08/23 DOTA
Python按行读取文件的实现方法【小文件和大文件读取】
2016/09/19 Python
完美解决torch.cuda.is_available()一直返回False的玄学方法
2021/02/06 Python
css3模拟jq点击事件的实例代码
2017/07/06 HTML / CSS
豪华床上用品、床单和浴室必需品:Peacock Alley
2019/09/04 全球购物
数据库什么时候应该被重组
2012/11/02 面试题
加拿大留学自荐信
2014/01/28 职场文书
2014年迎新年活动方案
2014/02/19 职场文书
大学生活自我评价
2014/04/09 职场文书
二年级学生评语大全
2014/04/23 职场文书
好书伴我成长演讲稿
2014/05/14 职场文书
教师“一帮一”结对子活动总结
2015/05/07 职场文书
机关单位2016年法制宣传日活动总结
2016/04/01 职场文书
导游词之山西祁县乔家大院
2019/10/14 职场文书
导游词之山东八仙过海景区
2019/11/11 职场文书
导游词之徐州云龙湖
2019/11/19 职场文书
让文件路径提取变得更简单的Python Path库
2021/05/27 Python
基于Go语言构建RESTful API服务
2021/07/25 Golang
各国货币符号大全
2022/02/17 杂记
一文搞懂PHP中的抽象类和接口
2022/05/25 PHP