Kuntau
Kuntau

Reputation: 761

PHP Accessing Parent Class Variable

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();

Question: How do I display the parent variable in the child? the expected result will echo 'parent bb'

Upvotes: 50

Views: 127519

Answers (9)

2hamed
2hamed

Reputation: 9067

all the properties and methods of the parent class are inherited in the child class so theoretically you can access them in the child class but beware of using the protected keyword in your class because it throws a fatal error when used in the child class.
as mentioned in php.net

The visibility of a property or method can be defined by prefixing the declaration with the keywords public, protected or private. Class members declared public can be accessed everywhere. Members declared protected can be accessed only within the class itself and by inherited and parent classes. Members declared as private may only be accessed by the class that defines the member.

Upvotes: 2

S_K
S_K

Reputation: 31

Through the parent class constructor, you can pass data to the parent class from the child class. Have a look below example for a better understanding

<?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;

?>

Upvotes: 0

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();

change the visibility private or protected for test

Upvotes: -1

Steve Nguyen
Steve Nguyen

Reputation: 5974

$bb has now become the member of class B after extending class A.

So you access $bb like it's an attribute of class 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();

Upvotes: 4

Manju Bhargavi Arveti
Manju Bhargavi Arveti

Reputation: 11

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();

Upvotes: 1

user2147836
user2147836

Reputation: 81

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();`

Upvotes: 4

Nicola Peluchetti
Nicola Peluchetti

Reputation: 76910

Just echo it since it's inherited

echo $this->bb;

Upvotes: 7

George Cummins
George Cummins

Reputation: 28936

echo $this->bb;

The variable is inherited and is not private, so it is a part of the current object.


Here is additional information in response to your request for more information about using parent:::

Use parent:: when you want add extra functionality to a method from the parent class. For example, imagine an Airplane class:

class Airplane {
    private $pilot;

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

Now suppose we want to create a new type of Airplane that also has a navigator. You can extend the __construct() method to add the new functionality, but still make use of the functionality offered by the parent:

class Bomber extends Airplane {
    private $navigator;

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

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

In this way, you can follow the DRY principle of development but still provide all of the functionality you desire.

Upvotes: 85

corretge
corretge

Reputation: 1759

With parent::$bb; you try to retrieve the static constant defined with the value of $bb.

Instead, do:

echo $this->bb;

Note: you don't need to call parent::_construct if B is the only class that calls it. Simply don't declare __construct in B class.

Upvotes: 6

Related Questions