Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A private method in PHP is a class method bound by the most restrictive visibility modifier (private). In PHP, visibility is resolved at the class level, not the instance level. This means a private method is strictly encapsulated within the defining class and cannot be accessed, invoked, or inherited by external contexts, child classes (subclasses), or parent classes. However, because visibility is class-bound, any method within a class can invoke the private methods of other instances of that exact same class.

Syntax

The method is declared using the private keyword preceding the function keyword. It can be applied to both instance and static methods.
class ExampleClass {
    private function instanceMethod(): void {
        // Implementation
    }

    private static function staticMethod(): void {
        // Implementation
    }
}

Visibility and Access Rules

1. Class-Level Internal Access Private methods can be invoked from anywhere within the exact class scope in which they are defined. Because PHP resolves visibility per class rather than per object, access is not restricted solely to the $this pseudo-variable or the self keyword. An object can call a private method on another distinct object, provided both instances belong to the exact same class.
class Entity {
    private function getInternalState(): string {
        return "Active";
    }

    private static function log(): void {
        echo "Logged.";
    }

    public function compare(Entity $otherEntity): void {
        // Permitted: Accessing private method via $this
        $localState = $this->getInternalState();
        
        // Permitted: Accessing private method of another instance of the same class
        $remoteState = $otherEntity->getInternalState(); 

        // Permitted: Accessing static private method via self
        self::log();
    }
}
2. External Access Attempting to call a private method from an object instance in the global scope or any other external context results in a fatal error.
$entity = new Entity();
$entity->compare(new Entity()); // Success
$entity->getInternalState(); // Fatal error: Uncaught Error: Call to private method Entity::getInternalState()

Inheritance Behavior

Unlike protected or public methods, private methods are not inherited by child classes. They remain entirely invisible to the subclass. If a child class declares a method with the exact same name as a private method in its parent class, it does not constitute method overriding. Instead, the child class is defining a completely new, independent method within its own scope. This behavior is known as method shadowing.
class ParentClass {
    public function callAction(): void {
        $this->action(); 
    }

    private function action(): void {
        echo "Parent action executed.\n";
    }
}

class ChildClass extends ParentClass {
    // This is a new method, not an override of ParentClass::action()
    private function action(): void {
        echo "Child action executed.\n";
    }
}

$child = new ChildClass();
// Outputs "Parent action executed." because ParentClass::callAction() 
// is bound to the ParentClass scope where its own private action() exists.
$child->callAction(); 

Reflection Access

While strictly enforced by the PHP engine during standard execution, private methods can be bypassed and invoked dynamically at runtime using PHP’s Reflection API.
class Target {
    private function hiddenMethod(): string {
        return "Accessed via Reflection";
    }
}

$target = new Target();
$reflectionMethod = new ReflectionMethod(Target::class, 'hiddenMethod');

// Prior to PHP 8.1, setAccessible(true) was required.
// As of PHP 8.1, ReflectionMethod::invoke() bypasses visibility automatically.
echo $reflectionMethod->invoke($target); 
Master PHP with Deep Grasping Methodology!Learn More