静态方法与非静态方法的调用
<?php
//php 5.6
//静态方法
//静态变量
//非静态方法
//非静态变量
//静态调用
//非静态调用
class A {
public static $staticA='staticA';
public $paramA='paramA';
public static function staticCall() {
self::test();
static::test();
var_dump(self::$staticA,static::$staticA);
//var_dump(self::$paramA,static::$paramA); // ① Fatal error: Access to undeclared static property: A::$paramA
//$this->paramA;//Fatal error: Using $this when not in object context
}
public static function testStatic() {
echo __METHOD__.'<br />';
}
public function test() {
echo __METHOD__.'<br />';
}
public function commonCall() {
self::testStatic();
static::testStatic();
var_dump(self::$staticA,static::$staticA);
self::test();
static::test();
//var_dump(self::$paramA,static::$paramA); //Fatal error: Access to undeclared static property: A::$paramA
$this->testStatic();
var_dump($this->$staticA);
}
}
A::staticCall();
echo '<hr />';
//A::commonCall();
//可以调用,Strict standards: Non-static method A::commonCall() should not be called statically
//静态调用静态方法,正常
//静态调用非静态方法,Strict standards: Non-static method A::test()
//$this未在Fatal error: Using $this when not in object context
echo '<hr />';
$a=new A();
$a->commonCall();
//静态调用静态方法、静态变量,正常
//静态调用非静态方法,正常
//无法静态调用非静态变量
//非静态调用静态方法,正常
//非静态调用静态变量,Fatal error
没有实例化($this还未定义),不能调用非静态属性或方法 –> $this不能出现在静态方法中
静态环境(静态方法中)
1.1 静态调用静态方法:self、static
静态变量:self、static
非静态方法:self、static(当一个类中有非静态方法被self::
调用时,系统会自动将这个方法转换为静态方法?,但会报Strict standards)
非静态变量:X1.2 非静态调用
静态方法:X
静态变量:X
非静态方法:X
非静态变量:X非静态环境(非静态方法中)
2.1 静态调用静态方法:self、static
静态变量:self、static
非静态方法:self、static(不报任何错误)
非静态变量:X2.2 非静态调用
静态方法:$this
静态变量:X
非静态方法: $this
非静态变量 :$this
后期静态绑定
自 PHP 5.3.0 起,PHP 增加了一个叫做后期静态绑定的功能,用于在继承范围内引用静态调用的类。
该功能从语言内部角度考虑被命名为“后期静态绑定”。“后期绑定”的意思是说,static:: 不再被解析为定义当前方法所在的类,而是在实际运行时计算的。也可以称之为“静态绑定”,因为它可以用于(但不限于)静态方法的调用。
self:: 的限制
使用 self:: 或者 CLASS 对当前类的静态引用,取决于定义当前方法所在的类。
后期静态绑定的用法
后期静态绑定本想通过引入一个新的关键字表示运行时最初调用的类来绕过限制。简单地说,这个关键字能够让你在上述例子中调用 test() 时引用的类是 B 而不是 A。最终决定不引入新的关键字,而是使用已经预留的 static 关键字。
class A {
public static function who() {
echo __CLASS__;
}
public static function test() {
self::who();//self取决于定义当前方法所在的类
static::who(); // 后期静态绑定从这里开始
}
}
class B extends A {
public static function who() {
echo __CLASS__;
}
}
B::test();
转发调用、非转发调用
准确说,后期静态绑定工作原理是存储了在上一个“非转发调用”(non-forwarding call)中的类名(late static bindings work by storing the class named in the last “non-forwarding call”)。当进行静态方法调用时,该类名即为明确指定的那个(通常在 :: 运算符左侧部分);当进行非静态方法调用时,即为该对象所属的类。所谓的“转发调用”(forwarding call)指的是通过以下几种方式进行的静态调用:self::,parent::,static:: 以及 forward_static_call()。可用 get_called_class() 函数来得到被调用的方法所在的类名,static:: 则指出了其范围(static:: introduces its scope)。
静态调用、self::、parent::、static::、forward_static_call()。在进行静态调用时未指名类名则是转发调用。
对应的非转发调用就是明确指定类名的静态调用和非静态调用($foobar->foobar()调用形式)。
转发与非转发调用本质区别在于是否将调用者的信息传递下去。这个概念只有在使用lsb的时候才有意义。
class A {
public static function foo() {
static::who();
}
public static function who() {
echo __CLASS__."<br />";
}
}
class B extends A {
public static function test() {
A::foo();
parent::foo();
self::foo();
}
public static function who() {
echo __CLASS__."<br />";
}
}
class C extends B {
public static function who() {
echo __CLASS__."<br />";
}
}
C::test();
首先C::test(),进入test方法,A::foo()、parent::foo()、self::foo()这三个方法调用的“上一次非转发调用”存储的类名就是C啦。
通过A::foo()进入foo方法时,“上一次非转发调用”存储的类名就变成A啦。所以static::实际上代表A::。
通过parent::foo()进入foo方法时,“上一次非转发调用”存储的类名还是C。所以static::代表C::。(parent转发了上一次的(非转发)调用)
通过self::foo()进入foo方法时,“上一次非转发调用”存储的类名还是C。所以static::代表C::。(self转发了上一次的(非转发)调用)
注意这里之所以会触发lsb是因为foo()方法中的static::用法。如果这里的static::who()改为self::who(),则不会出lsb,也就没有“上一次非转发调用”存储的类名这个概念了,实际上就是A::who()