小编Kun*_*tau的帖子

PHP访问父类变量

class A {
    private $aa;
    protected $bb = 'parent bb';

    function __construct($arg) {
       //do something..
    }

    private function parentmethod($arg2) {
       //do something..
    }
}

class B extends A {
    function __construct($arg) {
        parent::__construct($arg);
    }
    function childfunction() {
        echo parent::$bb; //Fatal error: Undefined class constant 'bb' 
    }
}

$test = new B($some);
$test->childfunction();
Run Code Online (Sandbox Code Playgroud)

问题:如何在孩子中显示父变量?预期结果将回应'父母bb'

php variables class parent

36
推荐指数
3
解决办法
9万
查看次数

标签 统计

class ×1

parent ×1

php ×1

variables ×1