从多类继承(PHP)访问静态变量
如何从C类访问$var,我尝试使用parent::parent::$var,但这不起作用从多类继承(PHP)访问静态变量,php,inheritance,Php,Inheritance,如何从C类访问$var,我尝试使用parent::parent::$var,但这不起作用 <?php class A { protected static $var = null; public function __construct(){ self::$var = "Hello"; } } class B extends A { parent::__construct(); //without using an inter
<?php
class A {
protected static $var = null;
public function __construct(){
self::$var = "Hello";
}
}
class B extends A {
parent::__construct();
//without using an intermediate var. e.g: $this->var1 = parent::$var;
}
class C extends B {
//need to access $var from here.
}
?>
由于变量是静态的,您可以按如下方式访问变量-
A::$var;
只要属性没有声明为private,就可以使用
self::
。除私有属性之外的任何内容都可以从层次结构中的任何位置获得
class A {
protected static $var = null;
public function __construct(){
self::$var = "Hello";
}
}
class B extends A {
}
class C extends B {
function get() { echo self::$var; }
}
(new C)->get();
你好
看