new static() 和 new self() 的区别异同
长夜漫漫啊!
今天领导本地搭建一个站。发现用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"
*/
new static() 和 new self() 的区别异同的更多相关文章
- PHP中new static()与new self()的区别异同分析
本文实例讲述了PHP中new static()与new self()的区别异同,相信对于大家学习PHP程序设计能够带来一定的帮助. 问题的起因是本地搭建一个站.发现用PHP 5.2 搭建不起来,站PH ...
- PHP中new static()与new self()的区别异同
self - 就是这个类,是代码段里面的这个类. static - PHP 5.3加进来的只得是当前这个类,有点像$this的意思,从堆内存中提取出来,访问的是当前实例化的那个类,那么 static ...
- static变量与普通变量的异同
1.static局部变量与普通局部变量的异同 相同点:都是局部变量,在函数内部定义,仅能被该模块内部的语句所访问. 不同点: 1)内存分配与释放: static修饰的局部变量在内存中存放在静态存储区, ...
- Java中static final 与 final 的区别(转载)
转自:http://advance0683.iteye.com/blog/1107732 Java中static final 与 final 的区别: 例子: Java代码 import java.u ...
- static final 和final的区别
学习java的时候常常会被修饰符搞糊涂,这里总结下static final和final的区别. static是静态修饰关键字,可以修饰变量和程序块以及类方法: 当定义一个static的变量的时候jvm ...
- 转(static final 和final的区别)
学习java的时候常常会被修饰符搞糊涂,这里总结下static final和final的区别. 1.static 强调只有一份,final 说明是一个常量,final定义的基本类型的值是不可改变的,但 ...
- PHP 中 new static 和 new self 的区别
今天老大在公司 问了一下 new static 和 new self 的区别 公司十个程序 竟然没有一个回答上来 后面画面自补 ... 本屌丝回家后 就百度了解了下 这二者区别 : ...
- C++ static全局变量与全局变量的区别/static全局函数与全局函数的区别
全局变量(外部变量)的说明之前再冠以static 就构成了静态的全局变量.全局变量本身就是静态存储方式, 静态全局变量当然也是静态存储方式.这两者在存储方式上并无不同.这两者的区别虽在于非静态全局变量 ...
- 面向对象之静态方法(static)和实例化方法的区别
这是一个经常被时时提出来的问题,很多时候我们以为理解了.懂了,但深究一下,我们却发现并不懂. 方法是我们每天都在写得,很多程序员大多都使用实例化方法,而很少使用静态方法,问原因也说不出来所以然,或者简 ...
随机推荐
- C++容器类的简介
C++容器类的简介 一.原型与构造函数 Vector的原型可定义为 vector<T, allocator <T> > 其构造函数为 vector() ...
- zoj 3820 Building Fire Stations 树的中心
Building Fire Stations Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge ...
- git reset and git checkout
git reset --hard <commit>: 1.替换引用的指向.引用指向新的提交ID; 2.替换暂存区.替换后,暂存区的内容和引用指向的文件夹树一致; 3.替换工作区.替换后,工 ...
- Android权限设置android.permission完整列表
android.permission.ACCESS_CHECKIN_PROPERTIES允许读写访问"properties”表在checkin数据库中,改值可以修改上传( Allows re ...
- istringstream
编写程序,将来自一个文件中的行保存在一个vector<string>中,然后使用一个istringstream从vector读取数据成员,每次读取一个单词 #include <ios ...
- Java_生产者消费者模式
/* * To change this license header, choose License Headers in Project Properties. * To change this t ...
- How to setup SLF4J and LOGBack in a web app - fast--转载
原文:https://wiki.base22.com/display/btg/How+to+setup+SLF4J+and+LOGBack+in+a+web+app+-+fast Logback is ...
- MySQL入门笔记
MySQL入门笔记 版本选择: 5.x.20 以上版本比较稳定 一.MySQL的三种安装方式: 安装MySQL的方式常见的有三种: · rpm包形式 · 通用二进制 ...
- 将WSDL文件生成的Java文件
- 不规则三角网(TIN)(转)
来自:http://blog.csdn.net/kikitamoon/article/details/8217641 Ⅰ 数字高程模型(DEM) 地球表面高低起伏,呈现一种连续变化的曲面,这种曲面无法 ...