PHP Accessing Parent Class Variable
Asked Answered
D

9

50
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'

Dixon answered 23/6, 2011 at 15:49 Comment(0)
S
84
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.

Soloma answered 23/6, 2011 at 15:50 Comment(2)
so thats mean parent keyword only used to access parent method?Dixon
Usually, you would use parent:: when you want to override a parent method, but still reference the parent's functionality. If you just want to call the parent's method, you do it the same way as for a variable: $this->parentmethod()Soloma
S
7

Just echo it since it's inherited

echo $this->bb;
Sabbath answered 23/6, 2011 at 15:51 Comment(3)
Just make sure that variable is not defined as private.Cowbell
I cannot get this to work. The variable is public, but the variables just aren't getting inherited.Maitilde
Apparently it's not. I assumed it would be but found they don't get inherited.Maitilde
P
6

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.

Parfleche answered 23/6, 2011 at 16:53 Comment(0)
M
4

$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();
Marthena answered 23/6, 2011 at 15:51 Comment(0)
T
4
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();`
Tortola answered 8/3, 2013 at 9:28 Comment(4)
parent::$this->bb has got to be the craziest object-oriented syntax I've ever seenSupposal
parent:: in parent::$this->bb achieves nothing but confusing the person asking. I recommend removal or further explanation in order to reduce complexity of the answer.Megass
this doesn't work Fatal error: Uncaught Error: Access to undeclared static property: A::$this in /Applications/MAMP/htdocs/native_testing/index.php:24 Stack trace: #0 /Applications/MAMP/htdocs/native_testing/index.php(29): B->childfunction() #1 {main} thrown in /Applications/MAMP/htdocs/native_testing/index.php on line 24Heterochromosome
This code returns Fatal error: Access to undeclared static property: A::$thisMaitilde
P
2

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.

Pettigrew answered 23/6, 2011 at 15:59 Comment(1)
protected doesn't throw errors when used in inherited and parent classes.Undersheriff
T
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();
Tollman answered 25/7, 2021 at 12:2 Comment(1)
Welcome to Stackoverflow. Please read this guide on how to answer questions: stackoverflow.com/help/how-to-answerSunda
L
0

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;

?>
Laity answered 7/12, 2021 at 4:7 Comment(0)
N
-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();

change the visibility private or protected for test

Nolin answered 19/7, 2022 at 14:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.