Properties
suggest changeClass member variables in PHP are referred to as properties. Over recent PHP releases (PHP 7.4 through PHP 8.4), properties have evolved from untyped, dynamic variables into a robust type system featuring strict types, immutability, constructor promotion, and property hooks.
1. Declaring Properties and Visibility
Properties must be declared with one of three visibility keywords:
public: Accessible from anywhere.protected: Accessible within the declaring class and subclasses.private: Accessible only within the declaring class.
<?php
class User
{
public string $name;
protected string $email;
private string $passwordHash;
public function __construct(string $name, string $email, string $password)
{
$this->name = $name;
$this->email = $email;
$this->passwordHash = password_hash($password, PASSWORD_DEFAULT);
}
}
2. Typed and Nullable Properties
Beginning with PHP 7.4, properties can declare explicit types, including union types (PHP 8.0) and intersection types (PHP 8.1):
<?php
class Product
{
// Typed scalar property with default value
public int $id = 0;
// Nullable type
public ?string $description = null;
// Union type
public int|float $price;
// Uninitialized property: accessing before initialization throws an Error
public string $sku;
}
$product = new Product();
// Accessing $product->sku here throws Error: Typed property Product::$sku must not be accessed before initialization
3. Constructor Property Promotion
Introduced in PHP 8.0, constructor property promotion dramatically reduces boilerplate by defining and initializing properties directly in the constructor signature:
<?php
class Customer
{
public function __construct(
public readonly int $id,
public string $name,
public string $email,
private string $billingToken,
protected int $rewardPoints = 0
) {
// Properties are automatically declared and assigned
}
}
$customer = new Customer(1, 'Alice Smith', 'alice@example.com', 'tok_12345');
echo $customer->name; // Alice Smith
4. Readonly Properties and Classes
PHP 8.1 introduced the readonly modifier. Once initialized, a readonly property cannot be modified:
<?php
class Money
{
public readonly int $amountInCents;
public readonly string $currency;
public function __construct(int $amountInCents, string $currency)
{
$this->amountInCents = $amountInCents;
$this->currency = strtoupper($currency);
}
}
$price = new Money(2500, 'USD');
// $price->amountInCents = 3000; // Fatal Error: Cannot modify readonly property Money::$amountInCents
In PHP 8.2+, you can mark the entire class as readonly, automatically applying readonly to every property:
<?php
readonly class Point
{
public function __construct(
public float $x,
public float $y
) {}
}
5. Static Properties
Static properties belong to the class definition rather than individual object instances:
<?php
class DatabaseConnection
{
private static ?self $instance = null;
private static int $connectionCount = 0;
private function __construct()
{
self::$connectionCount++;
}
public static function getInstance(): self
{
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
public static function getConnectionCount(): int
{
return self::$connectionCount;
}
}
6. Asymmetric Visibility (PHP 8.4)
PHP 8.4 allows setting separate visibility for reading and writing a property:
<?php
class Order
{
// Publicly readable, but can only be modified internally
public private(set) string $status = 'pending';
public function markCompleted(): void
{
$this->status = 'completed';
}
}
$order = new Order();
echo $order->status; // Allowed: 'pending'
// $order->status = 'cancelled'; // Fatal Error: Cannot modify private(set) property Order::$status from global scope
7. Dynamic and Magic Property Access
PHP provides magic methods (__get, __set, __isset, and __unset) to intercept attempts to read or write undeclared or inaccessible properties:
<?php
class DynamicRecord
{
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;
}
public function __isset(string $name): bool
{
return isset($this->data[$name]);
}
public function __unset(string $name): void
{
unset($this->data[$name]);
}
}