【问题标题】:Access protected var of superclass in php在php中访问超类的受保护var
【发布时间】:2012-10-23 15:30:51
【问题描述】:

一个小的 php 问题的简短问题。

class topclass {
protected $test;
//....
}

class childclass extends topclass {`
public static function accessVariable(){

//HOW CAN I ACCESS THE $Test VARIABLE OF THE SUPERCLASS HERE?

}...

有人可以帮帮我吗?

非常感谢

【问题讨论】:

    标签: php protected superclass


    【解决方案1】:

    self::$testparent::$test 用于静态函数,$this->test 用于常规函数。受保护的变量在扩展类的范围内可用,私有变量不可用。

    使用self::$testparent::$test 之间的区别在于,如果您在子类中覆盖了$test,则在使用self::$test 时会得到覆盖的值。

    当然,如果您希望静态访问该属性,则需要将其声明为静态(即protected static $test)。

    【讨论】:

    • self::test 语句假定 test 是一个类常量
    猜你喜欢
    • 2014-05-14
    • 2013-05-27
    • 1970-01-01
    • 2012-12-01
    • 2017-12-09
    • 2023-04-09
    • 2016-08-13
    • 2017-01-12
    • 2012-11-25
    相关资源
    最近更新 更多