繁体   English   中英

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

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

我如何从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;

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

暂无
暂无

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

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