转载请注明文章出处:tlanyan.me/self-in-php…php
PHP群里有人询问self
关键字的用法,答案是比较明显的:静态成员函数内不能用this
调用非成员函数,但能够用self
调用静态成员函数/变量/常量;其余成员函数能够用self
调用静态成员函数以及非静态成员函数。随着讨论的深刻,发现self
并无那么简单。鉴于此,本文先对几个关键字作对比和区分,再总结self
的用法。ide
parent
、static
以及this
的区别要想将完全搞懂self
,要与parent
、static
以及this
区分开。如下分别作对比。函数
parent
self
与parent
的区分比较容易:parent
引用父类/基类被隐盖的方法(或变量),self
则引用自身方法(或变量)。例如构造函数中调用父类构造函数:this
class Base {
public function __construct() {
echo "Base contructor!", PHP_EOL;
}
}
class Child {
public function __construct() {
parent::__construct();
echo "Child contructor!", PHP_EOL;
}
}
new Child;
// 输出:
// Base contructor!
// Child contructor!
复制代码
static
static
常规用途是修饰函数或变量使其成为类函数和类变量,也能够修饰函数内变量延长其生命周期至整个应用程序的生命周期。可是其与self
关联上是PHP 5.3以来引入的新用途:静态延迟绑定。spa
有了static
的静态延迟绑定功能,能够在运行时动态肯定归属的类。例如:code
class Base {
public function __construct() {
echo "Base constructor!", PHP_EOL;
}
public static function getSelf() {
return new self();
}
public static function getInstance() {
return new static();
}
public function selfFoo() {
return self::foo();
}
public function staticFoo() {
return static::foo();
}
public function thisFoo() {
return $this->foo();
}
public function foo() {
echo "Base Foo!", PHP_EOL;
}
}
class Child extends Base {
public function __construct() {
echo "Child constructor!", PHP_EOL;
}
public function foo() {
echo "Child Foo!", PHP_EOL;
}
}
$base = Child::getSelf();
$child = Child::getInstance();
$child->selfFoo();
$child->staticFoo();
$child->thisFoo();
复制代码
程序输出结果以下:对象
Base constructor!
Child constructor!
Base Foo!
Child Foo!
Child Foo!
复制代码
在函数引用上,self
与static
的区别是:对于静态成员函数,self
指向代码当前类,static
指向调用类;对于非静态成员函数,self
抑制多态,指向当前类的成员函数,static
等同于this
,动态指向调用类的函数。生命周期
parent
、self
、static
三个关键字联合在一块儿看挺有意思,分别指向父类、当前类、子类,有点“过去、如今、将来”的味道。get
this
self
与this
是被讨论最多,也是最容易引发误用的组合。二者的主要区别以下:string
this
不能用在静态成员函数中,self
能够;self
,不要用$this::
或$this->
的形式;self
,只能用this
;this
要在对象已经实例化的状况下使用,self
没有此限制;self
抑制多态行为,引用当前类的函数;而this
引用调用类的重写(override)函数(若是有的话)。self
的用途看完与上述三个关键字的区别,self
的用途是否是呼之即出?一句话总结,那就是:self
老是指向“当前类(及类实例)”。详细说则是:
这几个关键字中,只有this
要加$
符号且必须加,强迫症表示很难受;
静态成员函数中不能经过$this->
调用非静态成员函数,可是能够经过self::
调用,且在调用函数中未使用$this->
的状况下还能顺畅运行。此行为貌似在不一样PHP版本中表现不一样,在当前的7.3中ok;
在静态函数和非静态函数中输出self
,猜猜结果是什么?都是string(4) "self"
,迷之输出;
return $this instanceof static::class;
会有语法错误,可是如下两种写法就正常:
$class = static::class;
return $this instanceof $class;
// 或者这样:
return $this instanceof static;
复制代码
因此这是为何啊?!