PHP Reference ยท Functions and Classes
PHP magic methods
Reference for PHP magic methods: __construct, __get, __set, __isset, __unset, __call, __callStatic, __toString, __invoke, __clone, __debugInfo and more.
Magic methods are methods with reserved names that start with two underscores. PHP calls them automatically at certain moments: when an object is created, when you read an inaccessible property, when you call a method that does not exist, when you convert an object to a string, and so on.
Declare them with the exact name and the signature PHP expects. Do not invent your own double-underscore names; they are reserved for PHP.
class Bag { private array $data = []; public function __set(string $name, mixed $value): void { $this->data[$name] = $value; } public function __get(string $name): mixed { return $this->data[$name] ?? null; } }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
| Method | Called when |
|---|---|
__construct(...) |
An object is created with new. |
__destruct() |
The last reference disappears or the script ends. |
__get(string $name) |
Reading an inaccessible or missing property. |
__set(string $name, mixed $value) |
Writing to an inaccessible or missing property. |
__isset(string $name): bool |
isset() or empty() on an inaccessible or missing property. |
__unset(string $name) |
unset() on an inaccessible or missing property. |
__call(string $name, array $args) |
Calling an inaccessible or missing instance method. |
__callStatic(string $name, array $args) |
Calling an inaccessible or missing static method (must be static). |
__toString(): string |
The object is used as a string. |
__invoke(...) |
The object is called like a function. See __invoke. |
__clone() |
After clone copies the object. See Object cloning. |
__serialize() / __unserialize() |
serialize() / unserialize(). |
__sleep() / __wakeup() |
Older serialization hooks; prefer __serialize. |
__set_state(array) |
Objects restored from var_export() output. |
__debugInfo() |
var_dump() builds its output. |
Magic methods must be public (PHP warns otherwise) and, where shown, return the stated type.
Usage notes
__getand__setrun only for inaccessible properties. Declared public properties bypass them.- Recursion guard. Inside
__get('x'), reading$this->xagain does not call__getagain (PHP guards the same name and treats it as a plain missing property), but reading a different missing name does. __toString()makes the class implementStringableautomatically; it must return a string and may throw exceptions since PHP 8.__callhides typos. A misspelled method name silently dispatches there; prefer real methods and use magic for proxies and fluent APIs.- IDE and static analysis cannot see magic members unless you add
@propertyand@methodannotations. - Performance. Magic access is slower than real members.
__destructtiming is not guaranteed at shutdown; do not rely on it for important work.__sleep/__wakeupare legacy;__serialize/__unserializetake priority when both exist.- Naming: PHP reserves every method name starting with
__.