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

Method overriding in PHP is an object-oriented programming mechanism where a subclass provides a specific implementation for a method that is already defined in its parent class. The overridden method in the child class must share the same name and a compatible signature with the parent method, effectively replacing the parent's behavior during runtime via dynamic dispatch.

## Signature Compatibility and Rules

When overriding a method, PHP enforces strict rules regarding the method signature to maintain the Liskov Substitution Principle (LSP):

* **Visibility:** The access modifier of the overriding method must be the same or less restrictive than the parent method. For example, a `protected` method can be overridden as `protected` or `public`, but never as `private`.
* **Covariant Return Types:** The child method can declare a return type that is narrower (more specific) than the parent method's return type.
* **Contravariant Parameters:** The child method can declare parameter types that are broader (less specific) than the parent method's parameter types.
* **Additional Parameters:** An overriding method is allowed to add new parameters to the signature, provided those new parameters have default values (i.e., they are strictly optional).
* **The `final` Keyword:** If a parent method is prefixed with the `final` keyword, it is locked and cannot be overridden. Attempting to do so throws a fatal error.
* **Constructor Exemption:** Constructors (`__construct`) are exempt from LSP signature compatibility rules. A child class can define a completely different constructor signature from its parent without throwing an error, unless the constructor is explicitly defined in an interface.

## Accessing the Parent Method

Overriding a method completely shadows the parent's implementation. In PHP, neither standard methods nor magic methods (such as `__construct`) implicitly invoke their parent counterparts. If the parent class's logic is required, the developer must explicitly call the shadowed parent method from within the child class using the `parent::` scope resolution operator.

## Syntax Visualization

```php theme={"dark"}
class ParentClass 
{
    public function __construct(int $id) 
    {
        // Base initialization logic
    }

    /**
     * Base method to be overridden.
     */
    protected function processData(array $payload): object 
    {
        // Casting an array to an object inherently creates a stdClass instance
        return (object) $payload;
    }

    /**
     * This method cannot be overridden by any child class.
     */
    final public function getIdentifier(): string 
    {
        return static::class;
    }
}

class ChildClass extends ParentClass 
{
    /**
     * Constructor Exemption:
     * The child constructor can have a completely different signature.
     */
    public function __construct(string $uuid) 
    {
        // Explicitly invoking the parent constructor is required to run its logic
        parent::__construct((int) $uuid);
    }

    /**
     * Overridden method demonstrating PHP's overriding rules:
     * 1. Visibility widened (protected -> public).
     * 2. Parameter type widened (array -> iterable) [Contravariance].
     * 3. New optional parameter added ($log).
     * 4. Return type narrowed (object -> stdClass) [Covariance].
     */
    public function processData(iterable $payload, bool $log = false): stdClass 
    {
        // Convert iterable to array to satisfy the parent's strict signature
        $arrayPayload = is_array($payload) ? $payload : iterator_to_array($payload);

        // Explicitly invoking the shadowed parent method
        $baseObject = parent::processData($arrayPayload);

        // Child-specific mutation
        $baseObject->isProcessed = true;
        $baseObject->isLogged = $log;

        // $baseObject is already a stdClass (from the parent's array-to-object cast),
        // satisfying the covariant return type declaration.
        return $baseObject;
    }
}
```

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