从multiplie类继承访问静态var(PHP)

时间:2018-06-15 16:10:59

标签: 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 intermediate var. e.g: $this->var1 = parent::$var; 
}

class C extends B {
    //need to access $var from here.
}

?>

2 个答案:

答案 0 :(得分:0)

由于变量是静态的,您可以访问变量,如下所示 -

A::$var;

答案 1 :(得分:0)

只要该属性未声明为私有,您就可以使用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();
  

您好

请参阅https://eval.in/1022037

相关问题