> ## 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 Protected Method

A protected method in PHP is a class method bound by an access modifier that restricts its visibility strictly to the defining class, its parent classes, and its derived (child) classes. Crucially, PHP's visibility model is class-scoped rather than instance-scoped. This means access restrictions are evaluated based on the type of the calling class, allowing objects of the exact same class type to access each other's protected methods.

## Syntax Declaration

The `protected` keyword is placed before the `function` keyword in the method signature.

```php theme={"dark"}
class BaseClass {
    protected function processData(): void {
        // Method implementation
    }
}
```

## Visibility and Access Mechanics

The PHP engine enforces strict invocation rules for protected methods during runtime:

1. **Internal Invocation:** The method can be called from within the class that defines it using the `$this->` pseudo-variable.
2. **Inherited Invocation:** Derived classes can invoke the method using `$this->` (if not overridden) or the `parent::` scope resolution operator.
3. **Cross-Instance Invocation:** Because visibility is evaluated at the class level, an object can invoke a protected method on a completely different instance, provided both instances share the same class type.
4. **External Invocation:** Attempting to call a protected method on an instantiated object from *outside* the class hierarchy (e.g., from the global scope) results in a fatal error.

```php theme={"dark"}
class Core {
    protected function initialize(): string {
        return "Initialized";
    }

    public function inspectOther(Core $otherInstance): string {
        // Valid: Cross-instance access. Calling a protected method on 
        // a different instance of the same class type.
        return $otherInstance->initialize();
    }
}

class Extension extends Core {
    public function run(): string {
        // Valid: Invoked from within a derived class
        return $this->initialize(); 
    }
}

$instanceA = new Extension();
$instanceB = new Core();

$instanceA->run();                  // Valid: Returns "Initialized"
$instanceB->inspectOther($instanceA); // Valid: Returns "Initialized" (Cross-instance)

// Fatal Error: Call to protected method Core::initialize() from global scope
$instanceA->initialize(); 
```

## Overriding Rules and Signature Validation

When a derived class overrides a protected method from a parent class, PHP enforces visibility constraints based on the Liskov Substitution Principle. The overriding method must either maintain the `protected` access level or weaken it to `public`. It cannot be strengthened to `private`.

```php theme={"dark"}
class ParentClass {
    protected function executeTask(): void {}
}

class ValidChild extends ParentClass {
    // Valid: Visibility weakened from protected to public
    public function executeTask(): void {}
}

class InvalidChild extends ParentClass {
    // Fatal Error: Access level to InvalidChild::executeTask() must be protected (as in class ParentClass) or weaker
    private function executeTask(): void {}
}
```

## Static Protected Methods

The `protected` modifier can be combined with the `static` keyword. The same visibility rules apply, but the method is bound to the class itself rather than an object instance. It is invoked using the `self::`, `static::` (for late static binding), or `parent::` operators.

```php theme={"dark"}
class AbstractFactory {
    protected static function getIdentifier(): string {
        return "Factory_A";
    }
}

class ConcreteFactory extends AbstractFactory {
    public static function build(): string {
        // Valid: Accessing protected static method from derived class
        return parent::getIdentifier();
    }
}
```

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