#684·orm

Property hooks from PHP 8.4

Author: hrachCreated Oct 25, 2024Updated Oct 29, 2024
Labelsquestion

As PHP 8.4 comes with property hooks we could remove virtual properties, as they would be completely modelled by a custom property with getter hook.

This:

php
/** ...
 * @property-read int                $age             {virtual}
 */
final class Author extends Entity
{
	protected function getterAge(): int
	{
		if ($this->born === null) {
			return 0;
		}

		return ((int) date('Y')) - ((int) $this->born->format('Y'));
	}
}

Would become

php
/** ...
 */
final class Author extends Entity
{
	public int $age {
		get {
			if ($this->born === null) {
				return 0;
			}

			return ((int) date('Y')) - ((int) $this->born->format('Y'));
		}
	}
}

Please note, that this would not mean removing custom getters/setters support, as they're still needed for non-virtual properties.