PHP Accessing Parent Class Variable

113,452

Solution 1

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.

Solution 2

Just echo it since it's inherited

echo $this->bb;

Solution 3

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.

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

Solution 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();`
Share:
113,452
Kuntau
Author by

Kuntau

MyObject = checkCondition: (num) -> return true if num is 5 getSum: (num) -> total = 0 total += i for i in [1..num] when @checkCondition i total

Updated on July 09, 2022

Comments

  • Kuntau
    Kuntau almost 2 years
    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 parent variable in child? expected result will echo 'parent bb'

  • Kuntau
    Kuntau almost 13 years
    so thats mean parent keyword only used to access parent method?
  • George Cummins
    George Cummins almost 13 years
    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()
  • Tobia
    Tobia over 7 years
    parent::$this->bb has got to be the craziest object-oriented syntax I've ever seen
  • T30
    T30 over 6 years
    protected doesn't throw errors when used in inherited and parent classes.
  • SEoF
    SEoF over 6 years
    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.
  • KD.S.T.
    KD.S.T. almost 6 years
    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 24
  • MilanG
    MilanG almost 6 years
    Just make sure that variable is not defined as private.
  • user2924019
    user2924019 about 5 years
    I cannot get this to work. The variable is public, but the variables just aren't getting inherited.
  • user2924019
    user2924019 about 5 years
    Apparently it's not. I assumed it would be but found they don't get inherited.
  • user2924019
    user2924019 about 5 years
    This code returns Fatal error: Access to undeclared static property: A::$this
  • Timothy Alexis Vass
    Timothy Alexis Vass almost 3 years
    Welcome to Stackoverflow. Please read this guide on how to answer questions: stackoverflow.com/help/how-to-answer