简体   繁体   English

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

[英]Access Static var from multiplie class inheritance (PHP)

How can I access $var from C class, i try with parent::parent::$var but this didn't work. 我如何从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.
}

?>

由于变量是静态的,因此您可以像下面那样访问变量-

A::$var;

As long as the property isn't declared as private, then you can just use self:: . 只要未将该属性声明为私有,就可以使用self:: Anything other than private properties are available from anywhere in the hierarchy. 层次结构中任何地方都可以使用私有属性以外的任何内容。

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();

Hello 你好

See https://eval.in/1022037 参见https://eval.in/1022037

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM