PHP Reference ยท Types and Type Handling
The PHP object type
Reference for the PHP object type: creating objects, handle semantics, cloning, stdClass, casting arrays to objects, and the object type declaration.
An object is an instance of a class. It bundles data (properties) with behavior (methods). Unlike arrays, objects are passed around as handles: when you assign an object to another variable or pass it to a function, both names refer to the same object, and a change made through one name is visible through the other.
PHP also has a built-in empty class, stdClass, used as a generic bag of properties. Casting an array to object creates one, and json_decode() returns one by default. Since PHP 7.2 you can also use object as a parameter, return or property type that accepts any object.
$user = new stdClass(); $user->name = "Ada"; echo $user->name; // Ada
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
$o = new ClassName(arguments); $o->property = $value; $value = $o->property; $o->method(); $o?->method(); // nullsafe $copy = clone $o; $o = (object) $array; $array = (array) $o; function f(object $x): object { /* any object */ }
| Part | Description |
|---|---|
new |
Creates an instance. With no constructor arguments the parentheses are optional in a new expression, but keep them for clarity. |
clone |
Makes a shallow copy; __clone() runs afterwards on the copy. |
stdClass |
The generic empty class. Dynamic properties are fine here and are never deprecated. |
(object) cast |
Array keys become property names. A scalar becomes a stdClass with one property called scalar; null becomes an empty object. |
Usage notes
- Handle semantics. Assignment copies the handle, not the object. Use
clonefor a copy.cloneis shallow: properties that hold objects still point to the same inner objects unless__clone()copies them. ==versus===.==is true for two objects of the same class with equal properties.===is true only when both sides are the very same instance. See Comparing objects.- Dynamic properties are deprecated on classes other than
stdClasssince PHP 8.2 (they still work but emit a deprecation notice). Declare properties, or add#[\AllowDynamicProperties]for legacy code. - Numeric property names. Casting
[1 => "a"]to an object gives a property named"1", which you read as$o->{"1"}. - Casting an object to an array exposes private and protected properties with mangled keys (they contain NUL bytes). Prefer
get_object_vars()inside the class or your owntoArray()method. - Objects cannot be cast to
intorfloat(you get a warning and1) and need__toString()to become a string. json_decode($json, true)returns arrays instead ofstdClassobjects, which is often easier to work with.- The
objecttype accepts instances of any class, including closures. It cannot be combined with a class name in a union (it would be redundant) and is rarely the best choice; prefer a specific class or interface. - Identity. Each object has a handle number internally, but never rely on it; use
===instead.