New in PHP 8.4 - Property Hooks
Published on 2024-11-23
Introduction to Property Hooks
PHP 8.4 introduces a game-changing feature called Property Hooks. This feature provides developers with the ability to define custom behaviors for getting, setting, and initializing properties.
It reduces the need for boilerplate code, simplifies class definitions, and allows for more expressive and concise property management in object-oriented programming.
Property Management in PHP 8.3
Before PHP 8.4, developers relied heavily on explicit getter and setter methods to manage property behaviors. This often resulted in verbose and repetitive code. Here's how property management was implemented in PHP 8.3:
interface AddressPhp83Contract
{
public function setStreet(string $value): void;
public function getStreet(): string;
public function setPostalCode(int $value): void;
public function getPostalCode(): int;
public function getFullAddress(): string;
}
class AddressPhp83 implements AddressPhp83Contract
{
private string $street;
private int $postalCode;
public function setStreet(string $value): void
{
$this->street = $value;
}
public function getStreet(): string
{
return $this->street;
}
public function setPostalCode(int $value): void
{
if (mb_strlen($value) !== 5) {
throw new \InvalidArgumentException('Must be 5 characters.');
}
$this->postalCode = $value;
}
public function getPostalCode(): int
{
return $this->postalCode;
}
public function getFullAddress(): string
{
return $this->street . ' ' . $this->postalCode;
}
}
$addressPhp83 = new AddressPhp83();
echo 'Street:';
$addressPhp83->setStreet('34 Sreet ABC');
echo $addressPhp83->getStreet();
echo 'Postal code:';
$addressPhp83->setPostalCode(38000);
echo $addressPhp83->getPostalCode();
echo 'Full address:';
echo $addressPhp83->getFullAddress();
How Property Hooks Work in PHP 8.4
With PHP 8.4, Property Hooks streamline property management by allowing you to define behaviors directly within the property declaration. This approach eliminates the need for separate getter and setter methods while providing more control and expressiveness. Here's the same example implemented using Property Hooks:
interface AddressPhp84Contract
{
public int $postalCode { set; }
public string $fullAddress { get; }
}
class AddressPhp84 implements AddressPhp84Contract
{
public string $street;
public int $postalCode {
set {
if (mb_strlen($value) !== 5) {
throw new \InvalidArgumentException('Must be 5 characters.');
}
$this->postalCode = $value;
}
}
public string $fullAddress {
get => $this->street . ' ' . $this->postalCode;
}
}
$addressPhp84 = new AddressPhp84();
echo 'Street:';
$addressPhp84->street = '34 Sreet ABC';
echo $addressPhp84->street;
echo 'Postal code:';
$addressPhp84->postalCode = 38000;
echo $addressPhp84->postalCode;
echo 'Full address:';
echo $addressPhp84->fullAddress;
GitHub Repository
Property Hooks - You can see all the code on my GitHub repository:
@s-damian - Property Hooks in PHP 8.4
✨ If you found this tutorial useful, please give me a star on my GitHub repository:
@s-damian - New Features in PHP 8.4
Other Tutorials on PHP 8.4
📝 See other articles on the same subject:
Additional Resources
Official PHP documentation: