Skip to content
Tutorials Center
My learning

Topics

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.

PHP
class User
{
    public string $name;
    public ?string $email = null;
    public array $roles = [];
}
$u = new User;
$u->name = "Ada";

Try it

PHP Types, defaults and uninitialized state

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.

PHP readonly and static properties

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

PHP
[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() is false for null and for uninitialized properties, while property_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[] = $x works as expected, but returning $this->items hands out a copy (arrays are values).
  • Computed or validated properties can use property hooks in PHP 8.4+.
  • Variable names: $obj->$name and $obj->{'my key'} access a property by dynamic name.