> ## 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.

# PHP Private Method

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.

```php theme={"dark"}
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.

```php theme={"dark"}
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.

```php theme={"dark"}
$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.

```php theme={"dark"}
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.

```php theme={"dark"}
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); 
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor PHP Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
