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

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

改着改着发现了一个地方

return new static($val);
 

这尼玛是神马,只见过

return new self($val);

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

  

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

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

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

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"
*/

  

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

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

---


PHP中new static()与new self()的区别异同分析的更多相关文章

  1. PHP中new static()与new self()的区别异同

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

  2. PHP 中 new static 和 new self 的区别

            今天老大在公司 问了一下  new static  和 new self 的区别 公司十个程序 竟然没有一个回答上来 后面画面自补 ... 本屌丝回家后 就百度了解了下 这二者区别 : ...

  3. PHP中const,static,public,private,protected的区别

    原文地址:http://small.aiweimeng.top/index.php/archives/54.html const: 定义常量,一般定义后不可改变static: 静态,类名可以访问pub ...

  4. new static() 和 new self() 的区别异同

    长夜漫漫啊! 今天领导本地搭建一个站.发现用PHP 5.2 搭建不起来,站PHP代码里面有很多5.3以上的部分,领导让苦逼我更改在5.2下能运行. 改着改着发现了一个地方 return new sta ...

  5. Android 中关于static的使用问题

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/5251564.html 项目中,在不停地接收串口数据很长一段时间(几小时)后,会偶然性的报错.初步排除了oom ...

  6. (转)Java中的static关键字解析

    转载: http://www.cnblogs.com/dolphin0520/p/3799052.html 一.static关键字的用途 在<Java编程思想>P86页有这样一段话: &q ...

  7. 关于Java中的static关键字

    Java中的 static 关键字,确实是一个关键的字(key word),今天就来总结一下它的用法,说说为什么关键. Java中的 static 关键字主要是用来做内存管理的.理解了这句话才能够比较 ...

  8. Java中的static关键字解析

    Java中的static关键字解析 static关键字是很多朋友在编写代码和阅读代码时碰到的比较难以理解的一个关键字,也是各大公司的面试官喜欢在面试时问到的知识点之一.下面就先讲述一下static关键 ...

  9. C++中的static关键字的总结

    C++的static有两种用法:面向过程程序设计中的static和面向对象程序设计中的static.前者应用于普通变量和函数,不涉及类:后者主要说明static在类中的作用. 1.面向过程设计中的st ...

随机推荐

  1. SQL Server生成数据库的数据字典存储过程

    use fpErp  --指定要生成数据字典的数据库 go SELECT 表名=case when a.colorder=1 then d.name else '' end, 表说明=case whe ...

  2. JavaScript onmousewheel鼠标滚轮示例

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  3. Codeforces 980F Cactus to Tree 仙人掌 Tarjan 树形dp 单调队列

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF980F.html 题目传送门 - CF980F 题意 给定一个 $n$ 个节点 $m$ 条长为 $1$ 的边 ...

  4. ORA-12154: TNS: 无法解析指定的连接标识符 问题

    ORA-12154: TNS: 无法解析指定的连接标识符 问题:https://zhidao.baidu.com/question/397519550.html

  5. 044 SimpleDateFormat的线程安全问题与解决方案

    这个问题,以前好像写过,不过现在这篇文章,有一个重现的过程,还是值得读一读的. URL:SimpleDateFormat的线程安全问题与解决方案

  6. day73 中间件 以及模板引擎

    模板引擎: 基本实用{{k1}}  if for 模板中自定义函数:操作步骤 1在已经注册的App中创建一个名字叫templates文件夹 2任意创建一个py文件, 3创建名字叫register 的L ...

  7. day38 mycql 初识概念,库(增删改查),表(增删改)以及表字段(增删改查),插入更新操作

    在Navicat中把已经生成的表逆向成模型 数据库上,右键-逆向数据库到模型 ego笔记: 增删改查 文件夹(库) 增 create database day43 charset utf8; 改 al ...

  8. vee-validate校验demo

    //太长了,把异步校验放到前面吧: VeeValidate.Validator.extend('checkLoginName',{ getMessage:function () { return &q ...

  9. HDU-1170的解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1170 题意:要求输入几个案例,每个案例包含操作符(+,-,*,/),操作数(两个整数).现在要求分别输 ...

  10. 关于go get安装git golang项目时报错的处理办法

    关于go get安装git golang项目时报错的处理办法 使用go get安装github上的项目时一般来说,不可避免会出错.各种错误的处理办法: 必须条件: 1.安装git并配置环境变量.下载地 ...