Home  >  Article  >  Backend Development  >  static - php静态变量

static - php静态变量

WBOY
WBOYOriginal
2016-06-06 20:11:35899browse

我看到别人的代码

<code> static::$aliases[$root]</code>

类的开头定了了一个 staitc 变量
在类中 调用static 变量的话
难道不是 self::$aliases 么
static::$aliases
这个是为什么呢?

回复内容:

我看到别人的代码

<code> static::$aliases[$root]</code>

类的开头定了了一个 staitc 变量
在类中 调用static 变量的话
难道不是 self::$aliases 么
static::$aliases
这个是为什么呢?

这是PHP种的后期静态绑定(Late Static Binding)的使用。
staticself的区别主要是,如果使用self,那么不管继承此类的子类如何重写覆盖静态方法,都会使用本类中定义的静态方法。而static却相反,会以子类中重写覆盖的方法为准(如果子类没重写按继承链向上查找)。对于静态属性的使用也是如此。

http://php.net/manual/zh/language.oop5.late-static-bindings.php

static和self在继承的时候会有区别

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn