PHP Property Hooks
last modified February 15, 2025
In this article, we show how to use property hooks
in PHP. Property
hooks are a new feature introduced in PHP 8.4 that allow developers to define
custom behavior for property access and modification. This feature provides
greater control over how properties are read and written, enabling more robust
and maintainable code.
Property hooks are particularly useful for enforcing validation, logging, or other custom logic when accessing or modifying properties.
Main Features of PHP Property Hooks
- Custom Getter Logic: Define custom behavior when reading a property.
- Custom Setter Logic: Define custom behavior when writing to a property.
- Validation: Enforce validation rules when setting property values.
- Logging: Log property access or modification for debugging or auditing purposes.
- Encapsulation: Encapsulate property access logic within the class, improving code organization.
Basic Usage of Property Hooks
The following example demonstrates how to define and use property hooks in PHP.
<?php class Product { private string $name; // Property hook for getting the name public function __get(string $property): mixed { if ($property === 'name') { return strtoupper($this->name); } } // Property hook for setting the name public function __set(string $property, mixed $value): void { if ($property === 'name') { if (strlen($value) < 3) { throw new Exception("Name must be at least 3 characters long."); } $this->name = $value; } } } $product = new Product(); $product->name = "Laptop"; // Set the name echo $product->name; // Get the name (outputs in uppercase)
In this program, the __get
and __set
magic methods are
used to define custom behavior for accessing and modifying the name
property. The __get
method converts the name to uppercase when
accessed, and the __set
method enforces a minimum length
requirement.
$ php main.php LAPTOP
Validation with Property Hooks
The following example demonstrates how to use property hooks to enforce validation rules when setting property values.
<?php class User { private int $age; // Property hook for setting the age public function __set(string $property, mixed $value): void { if ($property === 'age') { if ($value < 0 || $value > 120) { throw new Exception("Age must be between 0 and 120."); } $this->age = $value; } } // Property hook for getting the age public function __get(string $property): mixed { if ($property === 'age') { return $this->age; } } } $user = new User(); $user->age = 25; // Valid age echo $user->age; // Outputs: 25 // $user->age = 150; // Throws an exception
In this program, the __set
method enforces a validation rule for
the age
property, ensuring that the value is between 0 and 120. The
__get
method allows reading the age
property.
$ php main.php 25
Logging with Property Hooks
The following example demonstrates how to use property hooks to log property access and modification.
<?php class Account { private float $balance = 0.0; // Property hook for getting the balance public function __get(string $property): mixed { if ($property === 'balance') { echo "Balance accessed: {$this->balance}\n"; return $this->balance; } } // Property hook for setting the balance public function __set(string $property, mixed $value): void { if ($property === 'balance') { echo "Balance updated from {$this->balance} to $value\n"; $this->balance = $value; } } } $account = new Account(); $account->balance = 100.0; // Logs: Balance updated from 0 to 100 echo $account->balance; // Logs: Balance accessed: 100
In this program, the __get
and __set
methods log access and modification of the balance
property. This is useful for debugging or auditing purposes.
$ php main.php Balance updated from 0 to 100 Balance accessed: 100 100
Source
PHP Property Hooks - Documentation
In this article, we have shown how to use property hooks
in PHP to define custom behavior for property access and modification. Property hooks are a powerful tool for enforcing validation, logging, and encapsulating property logic.
Author
List all PHP tutorials.