如何访问子类php5中的受保护变量

时间:2015-06-03 13:23:32

标签: php oop

<?php
class a
{
    protected $c=10;

}
class b extends a{

    echo a::$c;

    }
    //$obj = new b();

?>

这里我试图在子类中打印父类变量。你能告诉我如何在PHP5中使用子类中的父类变量吗?

1 个答案:

答案 0 :(得分:1)

class parent {
    protected $field;
}
class child extends parent {
  public function test() {
    return $this->field;
  }
}

Doc:http://php.net/manual/en/language.oop5.visibility.php