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

Magic methods are special, reserved methods in PHP that override default language behavior when specific operations are performed on an object. They act as interceptors for object lifecycle events, property access, method invocation, and serialization, allowing developers to define custom internal logic for operations that would otherwise trigger an error or default engine handling. All magic methods are prefixed with a double underscore (`__`).

By design, these methods are implicitly invoked by the PHP engine during runtime evaluation, though they can be called explicitly. PHP reserves all function names starting with `__` for magic methods; developers must not create custom methods with this prefix.

## Architectural Rules

* **Visibility:** With the exception of `__construct()`, `__destruct()`, and `__clone()` (which can be protected or private), all magic methods must be declared as `public`.
* **Context:** Only `__callStatic()` and `__set_state()` are declared as `static`. All others operate within the object instance context (`$this`).
* **Signatures:** Magic methods have strict signature requirements, particularly regarding return types (e.g., `__toString()` must return a `string`).

## Syntax and Signatures

```php theme={"dark"}
class MagicMethodImplementation 
{
    // Object Lifecycle
    public function __construct(mixed ...$args) {}
    public function __destruct() {}
    public function __clone(): void {}

    // Property Overloading (Interception)
    public function __get(string $name): mixed {}
    public function __set(string $name, mixed $value): void {}
    public function __isset(string $name): bool {}
    public function __unset(string $name): void {}

    // Method Overloading (Interception)
    public function __call(string $name, array $arguments): mixed {}
    public static function __callStatic(string $name, array $arguments): mixed {}

    // Serialization
    public function __sleep(): array {}
    public function __wakeup(): void {}
    public function __serialize(): array {}
    public function __unserialize(array $data): void {}

    // Representation and Execution
    public function __toString(): string {}
    public function __invoke(mixed ...$arguments): mixed {}
    public static function __set_state(array $properties): object {}
    public function __debugInfo(): array {}
}
```

## Mechanical Definitions

### Object Lifecycle

* `__construct()`: Triggered automatically during object instantiation (`new ClassName()`).
* `__destruct()`: Triggered by the garbage collector when no references to the object remain, or during the script shutdown sequence.
* `__clone()`: Triggered immediately after an object is duplicated using the `clone` keyword, operating on the newly created instance.

### Property Overloading

* `__get()`: Triggered when reading data from inaccessible (protected/private) or non-existent properties.
* `__set()`: Triggered when writing data to inaccessible or non-existent properties.
* `__isset()`: Triggered by calling the language constructs `isset()` or `empty()` on inaccessible or non-existent properties.
* `__unset()`: Triggered by calling the language construct `unset()` on inaccessible or non-existent properties.

### Method Overloading

* `__call()`: Triggered when invoking inaccessible or non-existent methods in an object context. The `$arguments` parameter receives an enumerated array of the passed parameters.
* `__callStatic()`: Triggered when invoking inaccessible or non-existent methods in a static context (`ClassName::method()`).

### Serialization

* `__sleep()`: Triggered prior to execution of `serialize()`. It must return an array containing the names of all variables of the object that should be serialized.
* `__wakeup()`: Triggered immediately upon execution of `unserialize()`.
* `__serialize()`: (PHP 7.4+) Triggered by `serialize()`. It must return an associative array representing the object's state. If defined, it supersedes `__sleep()`.
* `__unserialize()`: (PHP 7.4+) Triggered by `unserialize()`. It receives the associative array generated by `__serialize()`. If defined, it supersedes `__wakeup()`.

### Representation and Execution

* `__toString()`: Triggered when an object is cast to a string or evaluated in a string context (e.g., `echo $obj`).
* `__invoke()`: Triggered when a script attempts to call an object instance as a function (e.g., `$obj()`).
* `__set_state()`: Triggered by `var_export()`. It receives an associative array of exported properties and must return an instance of the class.
* `__debugInfo()`: Triggered by `var_dump()`. It must return an array of properties that should be displayed in the dump output, overriding the default behavior of dumping all properties.

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