How to Access Parent Class Variables in PHP?

Susan Sarandon
Release: 2024-10-22 22:08:29
Original
257 people have browsed it

How to Access Parent Class Variables in PHP?

Access Parent Class Variable in PHP: A Comprehensive Guide

Accessing parent class variables in PHP is crucial for understanding inheritance and object-oriented programming concepts. In the given example below, we have two classes, A and B, with class A as the parent class and class B as the child class.

class A {
    private $aa;
    protected $bb = 'parent bb';
}

class B extends A {
    function childfunction() {
        echo parent::$bb; // Error: Undefined class constant 'bb'
    }
}
Copy after login

The issue arises when we attempt to access the protected variable (bb) of the parent class A from the child class B using parent::$bb. This results in a fatal error since $bb is defined as protected, which means it is accessible within the class itself and by subclasses, but not directly from outside the class.

Solution:

To resolve this issue, we can use $this keyword which refers to the current object:

echo $this->bb;
Copy after login

The $this keyword provides access to protected and private variables and methods within the current class, allowing us to display the value of the $bb variable in this case.

Additional Information: Using parent::

In addition to using $this, we can also utilize the parent:: syntax to access variables and methods from the parent class. Unlike $this, parent:: is used when we want to either override or extend functionality from the parent class.

For example, suppose we want to create a new class that extends the Airplane class and adds a navigator property:

class Airplane {
    private $pilot;
    function __construct($pilot) {
        $this->pilot = $pilot;
    }
}

class Bomber extends Airplane {
    private $navigator;
    function __construct($pilot, $navigator) {
        parent::__construct($pilot); // Call parent constructor
        $this->navigator = $navigator;
    }
}
Copy after login

In this scenario, we override the __construct() method in the Bomber class, but we still use parent::__construct() to ensure that the original functionality of the Airplane class is maintained.

Understanding the difference between $this and parent:: is essential for effectively leveraging inheritance in PHP and creating robust and maintainable code.

The above is the detailed content of How to Access Parent Class Variables in PHP?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!