PHP Reference ยท Functions and Classes
PHP class properties
Reference for PHP class properties: typed and untyped declarations, default values, readonly, static properties, uninitialized state and common errors.
A property is a variable that belongs to an object (or, if static, to the class). You declare it in the class body with a visibility modifier and, in modern code, a type. Access it with -> on an object or :: for static properties.
A typed property that has no default value starts uninitialized: reading it before assigning throws an Error. That is different from an untyped property, which starts as null.
class User { public string $name; public ?string $email = null; public array $roles = []; } $u = new User; $u->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
[visibility] [static] [readonly] [type] $name [= constant expression];
| Part | Description |
|---|---|
| Visibility | public, protected or private; var is a legacy alias for public. PHP 8.4 adds asymmetric forms such as public private(set). |
static |
One value shared by the class; access with Class::$name. |
readonly |
PHP 8.1+. Can be initialized once, from inside the class scope, and needs a type. |
type |
Any type except void, never and callable. Union, nullable and intersection types are allowed. |
| Default | Must be a constant expression; not a function call or new (use the constructor). |
Errors: reading an uninitialized typed property throws Error; assigning a value of the wrong type throws TypeError; modifying a readonly property throws Error; accessing a private or protected property from outside throws Error.
Usage notes
- Type everything. Typed properties catch bugs at the moment of assignment, not later when the value is used.
- Initialize in the constructor when no sensible default exists, or use constructor promotion.
isset()isfalsefornulland for uninitialized properties, whileproperty_exists()only checks that the property is declared.- Readonly is shallow. A readonly property holding an object cannot be replaced, but the object itself can still change. To "change" one, build a new object (a "wither" method).
- Readonly properties cannot have default values and cannot be unset after initialization.
- Dynamic properties (undeclared) are deprecated since PHP 8.2.
- Reference to an array property like
$this->items[] = $xworks as expected, but returning$this->itemshands out a copy (arrays are values). - Computed or validated properties can use property hooks in PHP 8.4+.
- Variable names:
$obj->$nameand$obj->{'my key'}access a property by dynamic name.