PHP访问父类变量

问题描述 投票:0回答:9
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();

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

php class variables parent
9个回答
84
投票
echo $this->bb;

变量是继承的,不是私有的,所以是当前对象的一部分


这里是响应您的请求的附加信息,以获取有关使用

parent::
的更多信息:

当您想要

add
额外功能到父类的方法时,请使用parent::。例如,想象一个
Airplane
类:

class Airplane {
    private $pilot;

    public function __construct( $pilot ) {
        $this->pilot = $pilot;
    }
}

现在假设我们要创建一种也有导航器的新型飞机。您可以扩展 __construct() 方法来添加新功能,但仍然可以使用父级提供的功能:

class Bomber extends Airplane {
    private $navigator;

    public function __construct( $pilot, $navigator ) {
        $this->navigator = $navigator;

        parent::__construct( $pilot ); // Assigns $pilot to $this->pilot
    }
}

通过这种方式,您可以遵循 DRY 原则 开发,但仍然提供您想要的所有功能。


7
投票

只是回应它,因为它是继承的

echo $this->bb;

6
投票

使用

parent::$bb;
尝试检索用
$bb
的值定义的静态常量。

相反,做:

echo $this->bb;

注意: 如果 B 是唯一调用它的类,则无需调用

parent::_construct
。只是不要在 B 类中声明 __construct。


5
投票
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::$this->bb; //works by M
    }
}

$test = new B($some);
$test->childfunction();`

4
投票

$bb现在扩展了class A后成为了class B的成员

所以你访问

$bb
就像它是 B 类的属性一样。

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 $this->bb; 
    }
}

$test = new B($some);
$test->childfunction();

2
投票

父类的所有属性和方法都在子类中继承,因此理论上您可以在子类中访问它们,但要注意在您的类中使用

protected
关键字,因为在子类中使用它会引发致命错误.
php.net

中所述

属性或方法的可见性 可以通过在 使用关键字 public 的声明, 受保护或私有。班级成员 声明为 public 可以访问 到处。成员声明受保护 只能在类内访问 本身以及继承和父母 类。成员声明为私有 只能由类访问 定义成员。


1
投票
PHP Accessing Parent Class Protected Variable & Methods
class A {
    protected $bb = 'parent bb';
    protected function sayHello(){
        echo 'Say Hello';
    }
}

class B extends A {
    public function childfunction() {
        echo $this->bb.'<br>'; 
        echo $this->sayHello();
    }
}

$test = new B();
$test->childfunction();

0
投票

通过父类构造函数,可以从子类向父类传递数据。看看下面的例子以获得更好的理解

<?php

class Student 
{
    public $name;
    function __construct($name){
        $this->name = $name;
    }
}

class Test extends Student
{
    public $age;
    function __construct($name,$age){
        $this->age = $age;
        parent::__construct($name);
    }
}

$obj = new Test("sajib khan" ,21);
echo $obj->name;
echo $obj->age;

?>

-1
投票
class A {
    private $points = 100;

    public function getPoints() {
        return $this->points;
    }
}

class B extends A {
    protected $points = 70;

    public function getPoints() {
        return parent::getPoints();
    }
}

$element = new B();
echo $element->getPoints();

更改私有或受保护的可见性以进行测试

© www.soinside.com 2019 - 2024. All rights reserved.