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.

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

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.
Master PHP with Deep Grasping Methodology!Learn More