
static::late statische Bindung mit neuem static() in PHP
1. self, $this, __CLASS__
1 2 3 4 5 6 7 8 9 10 11 12 13 | class A {}
class B extends A{
parent::( public | protected )(静态方法|静态变量) ===> parent永远是A
self::( public | protected )(静态方法|静态变量) ===> self永远是B
$this ->( public | protected )(非静态方法|非静态变量) ===> $this 永远是B的是实例化对象
__CLASS__ ===> 永远是B
}
class C extends B{
parent::( public | protected )(静态方法|静态变量) ===> parent永远是B
self::( public | protected )(静态方法|静态变量) ===> self永远是C
$this ->( public | protected )(非静态方法|非静态变量) ===> $this 永远是C的是实例化对象
__CLASS__ ===> 永远是C
}
|
Nach dem Login kopieren
2. static::
Das static-Schlüsselwort kann die folgenden Funktionen erreichen:
1 Aufruf Statische Methoden einer Klasse haben einen späten statischen Bindungseffekt.
2 Der Aufruf statischer Eigenschaften einer Klasse hat einen späten statischen Bindungseffekt.
3 Der Aufruf nicht statischer Methoden einer Klasse hat keinen späten statischer Bindungseffekt ;
4 Hinweis: Nicht statische Eigenschaften können nicht aufgerufen werden;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | class A {
private static function foo() {
echo "A success!\n" ;
}
public function test() {
$this ->foo();
}
}
class B extends A {
}
class C extends A {
private static function foo() {
echo "C success!\n" ;
}
}
$b = new B();
$b ->test();
$c = new C();
$c ->test();
|
Nach dem Login kopieren
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | class A {
private static function foo() {
echo "A success!\n" ;
}
public function test() {
static ::foo();
}
}
class B extends A {
}
class C extends A {
private static function foo() {
echo "C success!\n" ;
}
}
$b = new B();
$b ->test();
$c = new C();
$c ->test();
class A {
private static function foo() {
echo "A success!\n" ;
}
public function test() {
static ::foo();
}
}
class B extends A {
}
class C extends A {
public static function foo() {
echo "C success!\n" ;
}
}
$b = new B();
$b ->test();
$c = new C();
$c ->test();
|
Nach dem Login kopieren
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | class A {
public static function foo() {
static ::who();
}
public static function who() {
echo __CLASS__ . "\n" ;
}
}
class B extends A {
public static function test() {
A::foo();
parent::foo();
self::foo();
}
public static function who() {
echo __CLASS__ . "\n" ;
}
}
class C extends B {
public static function who() {
echo __CLASS__ . "\n" ;
}
}
C::test();
A =>A::foo()的结果
C =>parent::foo()能走到A的foo,里面 static ::who找C::who
C =>self::foo()能走到B的foo,B继承A,走到A的foo,里面 static ::who找C::who
class A {
protected static function foo() {
static ::who();
}
protected static function who() {
echo __CLASS__ . "\n" ;
}
}
class B extends A {
public static function test() {
A::foo();
parent::foo();
self::foo();
}
protected static function who() {
echo __CLASS__ . "\n" ;
}
}
class C extends B {
protected static function who() {
echo __CLASS__ . "\n" ;
}
}
C::test();
class A {
public static function foo() {
static ::who();
}
private static function who() {
echo __CLASS__ . "\n" ;
}
}
class B extends A {
public static function test() {
A::foo();
parent::foo();
}
private static function who() {
echo __CLASS__ . "\n" ;
}
}
class C extends B {
private static function who() {
echo __CLASS__ . "\n" ;
}
}
C::test();
|
Nach dem Login kopieren
3. new static()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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());
echo get_class(B::get_static());
echo get_class(A::get_static());
|
Nach dem Login kopieren
Empfohlenes Tutorial: "PHP-Video-Tutorial》
Das obige ist der detaillierte Inhalt vonSo erstellen Sie eine statische Bindung zwischen static:: und new static() in PHP. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!