PHP Accessing Parent Class Variable

PHP Accessing Parent Class Variable

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Why You Can't Access Private and Protected Properties Directly from Child Classes

Attempting to echo a parent class property directly from a child class, as shown in the childfunction method of class B, triggers a fatal error: Undefined class constant 'bb'. This behavior stems from how PHP resolves static context versus instance context when accessing properties.

In your specific code snippet, the line echo parent::$bb; uses the static scope operator (::). When you use this syntax without an instance reference, PHP treats $bb as a class constant rather than an instance property. Even though $bb is declared in class A, it is defined as a variable, not a constant. Because constants must be public and cannot be overridden by child classes, PHP has no way to resolve parent::$bb in the context of class B. The engine literally looks for a named constant called bb in class A and fails to find one, resulting in the fatal error.

To display the value 'parent bb', you must access the property through an instance of the parent class or via the current object's internal state, depending on your architectural needs. The solution depends heavily on whether $bb is meant to be shared across all instances or specific to the current object.

Accessing via Parent Instance Reference

If your goal is simply to read the value of $bb from within B, the most straightforward approach is to instantiate the parent class and access its property. However, this breaks encapsulation slightly and requires knowing that A has a public getter or a public property. Since $bb is marked as protected, you can access it if you hold a reference to a parent instance.

class B extends A {
    function __construct($arg) {
        parent::__construct($arg);
    }

    function childfunction() {
        // Create an instance of the parent class
        $parentInstance = new self(); 
        
        // Access protected property via the instance
        echo $parentInstance->bb; 
    }
}

While this works, calling new self() inside a method often feels inelegant and can lead to unexpected behavior if inheritance chains are complex. More importantly, this approach bypasses the intended access control of the protected keyword by forcing an explicit instantiation just for reading data.

The Proper Way: Using Getter Methods

The robust architectural pattern in object-oriented PHP is to define a getter method in the parent class. This respects the visibility modifiers (private, protected) and encapsulates the logic required to expose the data. Class A should define a public method that returns $bb.

class A {
    private $aa;
    protected $bb = 'parent bb';
    
    function __construct($arg) {
        // do something..
    }
    
    public function getBb() {
        return $this->bb;
    }

    private function parentmethod($arg2) {
        // do something..
    }
}

class B extends A {
    function __construct($arg) {
        parent::__construct($arg);
    }
    
    function childfunction() {
        echo parent::getBb();
    }
}

By defining getBb() in class A, you maintain control over how $bb is exposed. Class B calls this method using the static parent:: scope, which correctly resolves to the method defined in class A. This approach avoids the "Undefined class constant" error entirely because you are now invoking a function, not trying to access a non-existent constant.

Why Static Access Fails on Properties

It is crucial to understand why the original code failed. In PHP, properties are instance variables; they do not exist at the class level until an object is instantiated. Constants, however, exist at the class level and can be accessed statically. When you write parent::$bb, PHP attempts to resolve $bb as a constant on the A class. Since $bb was declared as a property (even with protected visibility), it does not satisfy the requirements for a static constant.

This distinction is a common pitfall when transitioning from languages where static and instance access are less strictly differentiated or when refactoring code that relies on magic methods like __get. If you need dynamic access, consider implementing __get() in the parent class, but always prefer explicit getters for clarity and performance.

For projects requiring complex inheritance hierarchies or strict type safety, leveraging Laravel's service container or dependency injection patterns can further abstract these interactions. You might find useful discussions on how to handle similar visibility issues in larger frameworks at https://laravelcompany.com.

Our network