」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > PHP 屬性掛鉤

PHP 屬性掛鉤

發佈於2024-08-26
瀏覽:757

PHP  Property Hooks

Introduction

PHP 8.4 will be released in November 2024 and will be bringing a cool new feature: property hooks.

In this article, we're going to take a look at what property hooks are and how you might use them in your PHP 8.4 projects.

As a side note, you might also be interested in checking out my other article which shows you the new array functions which are being added in PHP 8.4.

What are PHP Property Hooks?

Property hooks allow you to define custom getter and setter logic for class properties without having to write separate getter and setter methods. This means you can define the logic directly in the property declaration so you can directly access a property (like $user->firstName) without having to remember to call a method (like $user->getFirstName() and $user->setFirstName()).

You can check out the RFC for this feature at https://wiki.php.net/rfc/property-hooks

If you're a Laravel developer, as you're reading this article, you might notice that the hooks look very similar to accessors and mutators in Laravel models.

I quite like the look of the property hooks feature and I imagine it's something I'll be using in my projects when PHP 8.4 is released.

To understand how property hooks work, let's take a look at some example usage.

The "get" Hook

You can define a get hook that will be called whenever you try to access a property.

For example, imagine you have a simple User class that accepts a firstName and lastName in the constructor. You might want to define a fullName property that concatenates the first and last names together. To do this, you could define a get hook for the fullName property:

readonly class User
{
    public string $fullName {
        get {
            return $this->firstName.' '.$this->lastName;
        }
    }

    public function __construct(
        public readonly string $firstName,
        public readonly string $lastName
    ) {
        //
    }
}

$user = new User(firstName: 'ash', lastName: 'allen');

echo $user->firstName; // ash
echo $user->lastName; // allen
echo $user->fullName; // ash allen

In the example above, we can see that we've defined a get hook for the fullName property that returns a value which is calculated by concatenating the firstName and lastName properties together. We can clean this up a little bit more by using a syntax similar to arrow functions too:

readonly class User
{
    public string $fullName {
        get =>  $this->firstName.' '.$this->lastName;
    }

    public function __construct(
        public readonly string $firstName,
        public readonly string $lastName,
    ) {
        //
    }
}

$user = new User(firstName: 'ash', lastName: 'allen');

echo $user->firstName; // ash
echo $user->lastName; // allen
echo $user->fullName; // ash allen

Type Compatibility

It's important to note that the returned value from the getter has to be compatible with the type of the property.

If strict types aren't enabled, the value will be type-juggled to the property type. For example, if you return an integer from a property that is declared as a string, the integer will be converted to a string:

declare(strict_types=1);

class User
{
    public string $fullName {
        get {
            return 123;
        }
    }

    public function __construct(
        public readonly string $firstName,
        public readonly string $lastName,
    ) {
        //
    }
}

$user = new User(firstName: 'ash', lastName: 'allen');

echo $user->fullName; // "123"

In the above example, even though we've specified 123 as an integer to be returned, "123" is returned as a string because the property is a string.

We can add declare(strict_types=1); to the top of the code like so to enable strict type-checking:

declare(strict_types=1);

class User
{
    public string $fullName {
        get {
            return 123;
        }
    }

    public function __construct(
        public readonly string $firstName,
        public readonly string $lastName,
    ) {
        //
    }
}

Now this would cause an error to be thrown because the return value is an integer, but the property is a string:

Fatal error: Uncaught TypeError: User::$fullName::get(): Return value must be of type string, int returned

The "set" Hook

PHP 8.4 property hooks also allow you to define a set hook. This is called whenever you try to set a property.

You can choose between two separate syntaxes for the set hook:

  • Explicitly defining the value to set on the property
  • Using an arrow function to return the value to set on the property

Let's look at both of these approaches. We'll imagine we want to uppercase the first letters of the first and last name when they're set on the User class:

declare(strict_types=1);

class User
{   
    public string $firstName {
        // Explicitly set the property value
        set(string $name) {
            $this->firstName = ucfirst($name);
        }
    }

    public string $lastName {
        // Use an arrow function and return the value
        // you want to set on the property 
        set(string $name) => ucfirst($name);
    }

    public function __construct(
        string $firstName,
        string $lastName
    ) {
        $this->firstName = $firstName;
        $this->lastName = $lastName;
    }
}

$user = new User(firstName: 'ash', lastName: 'allen');

echo $user->firstName; // Ash
echo $user->lastName; // Allen

As we can see in the example above, we've defined a set hook for the firstName property that uppercases the first letter of the name before setting it on the property. We've also defined a set hook for the lastName property that uses an arrow function to return the value to set on the property.

Type Compatibility

If the property has a type declaration, then its set hook must have a compatible type set too. The following example will return an error because the set hook for firstName doesn't have a type declaration, but the property itself has a type declaration of string:

class User
{   
    public string $firstName {
        set($name) => ucfirst($name);
    }

    public string $lastName {
        set(string $name) => ucfirst($name);
    }

    public function __construct(
        string $firstName,
        string $lastName
    ) {
        $this->firstName = $firstName;
        $this->lastName = $lastName;
    }
}

Attempting to run the above code would result in the following error being thrown:

Fatal error: Type of parameter $name of hook User::$firstName::set must be compatible with property type

Using "get" and "set" Hooks Together

You aren't limited to using the get and set hooks separately. You can use them together in the same property.

Let's take a simple example. We'll imagine we have a fullName property on our User class. When we set the property, we'll split the full name into the first and last name. I know this is a naive approach and there are much better solutions, but it's purely for the sake of example to highlight the hooked properties.

The code may look something like so:

declare(strict_types=1);

class User
{
    public string $fullName {
        // Dynamically build up the full name from
        // the first and last name
        get => $this->firstName.' '.$this->lastName;

        // Split the full name into first and last name and
        // then set them on their respective properties
        set(string $name) {
            $splitName = explode(' ', $name);
            $this->firstName = $splitName[0];
            $this->lastName = $splitName[1];
        }
    }

    public string $firstName {
        set(string $name) => $this->firstName = ucfirst($name);
    }

    public string $lastName {
        set(string $name) => $this->lastName = ucfirst($name);
    }

    public function __construct(string $fullName) {
        $this->fullName = $fullName;
    }
}

$user = new User(fullName: 'ash allen');

echo $user->firstName; // Ash
echo $user->lastName; // Allen
echo $user->fullName; // Ash Allen

In the code above, we've defined a fullName property that has both a get and set hook. The get hook returns the full name by concatenating the first and last name together. The set hook splits the full name into the first and last name and sets them on their respective properties.

You may have also noticed that we're not setting a value on the fullName property itself. Instead, if we need to read the value of the fullName property, the get hook will be called to build up the full name from the first and last name properties. I've done this to highlight that you can have a property that doesn't have a value set directly on it, but instead, the value is calculated from other properties.

Using Property Hooks on Promoted Properties

A cool feature of property hooks is that you can also use them with constructor promoted properties.

Let's check out an example of a class that isn't using promoted properties and then look at what it might look like using promoted properties.

Our User class might look like so:

readonly class User
{
    public string $fullName {
        get => $this->firstName.' '.$this->lastName;
    }

    public string $firstName {
        set(string $name) => ucfirst($name);
    } 

    public string $lastName {
        set(string $name) => ucfirst($name);
    }

    public function __construct(
        string $firstName,
        string $lastName,
    ) {
        $this->firstName = $firstName;
        $this->lastName = $lastName;
    }
}

We could promote the firstName and lastName properties into the constructor and define their set logic directly on the property:

readonly class User
{
    public string $fullName {
        get => $this->firstName.' '.$this->lastName;
    }

    public function __construct(
        public string $firstName {
            set (string $name) => ucfirst($name);
        }, 
        public string $lastName {
            set (string $name) => ucfirst($name);
        }
    ) {
        //
    }
}  

Write-only Hooked Properties

If you define a hooked property with a setter that doesn't actually set a value on the property, then the property will be write-only. This means you can't read the value of the property, you can only set it.

Let's take our User class from the previous example and modify the fullName property to be write-only by removing the get hook:

declare(strict_types=1);

class User
{
    public string $fullName {
        // Define a setter that doesn't set a value
        // on the "fullName" property. This will
        // make it a write-only property.
        set(string $name) {
            $splitName = explode(' ', $name);
            $this->firstName = $splitName[0];
            $this->lastName = $splitName[1];
        }
    }

    public string $firstName {
        set(string $name) => $this->firstName = ucfirst($name);
    }

    public string $lastName {
        set(string $name) => $this->lastName = ucfirst($name);
    }

    public function __construct(
        string $fullName,
    ) {
        $this->fullName = $fullName;
    }
}

$user = new User('ash allen');

echo $user->fullName; // Will trigger an error!

If we were to run the code above, we'd see the following error being thrown when attempting to access the fullName property:

Fatal error: Uncaught Error: Property User::$fullName is write-only

Read-only Hooked Properties

Similarly, a property can be read-only.

For example, imagine we only ever want the fullName property to be generated from the firstName and lastName properties. We don't want to allow the fullName property to be set directly. We can achieve this by removing the set hook from the fullName property:

class User
{
    public string $fullName {
        get {
            return $this->firstName.' '.$this->lastName;
        }
    }

    public function __construct(
        public readonly string $firstName,
        public readonly string $lastName,
    ) {
        $this->fullName = 'Invalid'; // Will trigger an error!
    }
}

If we were to try and run the code above, the following error would be thrown because we're trying to set the fullName property directly:

Uncaught Error: Property User::$fullName is read-only

Using the "readonly" keyword

You can still make our PHP classes readonly even if they have hooked properties. For example, we may want to make the User class readonly:

readonly class User
{   
    public string $firstName {
        set(string $name) => ucfirst($name);
    }

    public string $lastName {
        set(string $name) => ucfirst($name);
    }

    public function __construct(
        string $firstName,
        string $lastName,
    ) {
        $this->firstName = $firstName;
        $this->lastName = $lastName;
    }
}

However, a hooked property cannot use the readonly keyword directly. For example, this class would be invalid:

class User
{
    public readonly string $fullName {
        get => $this->firstName.' '.$this->lastName;
    }

    public function __construct(
        string $firstName,
        string $lastName,
    ) {
        $this->firstName = $firstName;
        $this->lastName = $lastName;
    }
}

The above code would throw the following error:

Fatal error: Hooked properties cannot be readonly

The "PROPERTY" Magic Constant

In PHP 8.4, a new magic constant called __PROPERTY__ has been introduced. This constant can be used to reference the property name within the property hook.

Let's take a look at an example:

class User
{
    // ...

    public string $lastName {
        set(string $name) {
            echo __PROPERTY__; // lastName
            $this->{__PROPERTY__} = ucfirst($name); // Will trigger an error!
        }
    }

    public function __construct(
        string $firstName,
        string $lastName,
    ) {
        $this->firstName = $firstName;
        $this->lastName = $lastName;
    }
}

In the code above, we can see that using __PROPERTY__ inside the lastName property's setter will output the property name lastName. However, it's also worth noting that trying to use this constant in an attempt to set the property value will trigger an error:

Fatal error: Uncaught Error: Must not write to virtual property User::$lastName

There's a handy use case example for the __PROPERTY__ magic constant that you can check out on GitHub: https://github.com/Crell/php-rfcs/blob/master/property-hooks/examples.md.

Hooked Properties in Interfaces

PHP 8.4 also allows you to define publicly accessible hooked properties in interfaces. This can be useful if you want to enforce that a class implements certain properties with hooks.

Let's take a look at an example interface with hooked properties declared:

interface Nameable
{
    // Expects a public gettable 'fullName' property
    public string $fullName { get; }

    // Expects a public gettable 'firstName' property
    public string $firstName { get; }

    // Expects a public settable 'lastName' property
    public string $lastName { set; }
}

In the interface above, we're defining that any classes implementing the Nameable interface must have:

  • A fullName property that is at least publicly gettable. This can be achieved by defining a get hook or not defining a hook at all.
  • A firstName property that is at least publicly gettable.
  • A lastName property that is at least publicly settable. This can be achieved by defining a property which has a set hook or not defining a hook at all. But if the class is read-only then the property must have a set hook.

This class that implements the Nameable interface would be valid:

class User implements Nameable
{
    public string $fullName {
        get => $this->firstName.' '.$this->lastName;
    }

    public string $firstName {
        set(string $name) => ucfirst($name);
    }

    public string $lastName;

    public function __construct(
        string $firstName,
        string $lastName,
    ) {
        $this->firstName = $firstName;
        $this->lastName = $lastName;
    }
}

The class above would be valid because the fullName property has a get hook to match the interface definition. The firstName property only has a set hook, but is still publicly accessible so it satisfies the criteria. The lastName property doesn't have a get hook, but it is publicly settable so it satisfies the criteria.

Let's update our User class to enforce a get and set hook for the fullName property:

interface Nameable
{
    public string $fullName { get; set; }

    public string $firstName { get; }

    public string $lastName { set; }
}

Our User class would no longer satisfy the criteria for the fullName property because it doesn't have a set hook defined. It would cause the following error to be thrown:

Fatal error: Class User contains 1 abstract methods and must therefore be declared abstract or implement the remaining methods (Nameable::$fullName::set)

Hooked Properties in Abstract Classes

Similar to interfaces, you can also define hooked properties in abstract classes. This can be useful if you want to provide a base class that defines hooked properties that child classes must implement. You can also define the hooks in the abstract class and have them be overridden in the child classes.

For example, let's make a Model abstract class that defines a name property that must be implemented by child classes:

abstract class Model
{
    abstract public string $fullName {
        get => $this->firstName.' '.$this->lastName;
        set;
    }

    abstract public string $firstName { get; }

    abstract public string $lastName { set; }
}

In the abstract class above, we're defining that any classes that extend the Model class must have:

  • A fullName property that is at least publicly gettable and settable. This can be achieved by defining a get and set hook or not defining a hook at all. We've also defined the get hook for the fullName property in the abstract class so we don't need to define it in the child classes, but it can be overridden if needed.
  • A firstName property that is at least publicly gettable. This can be achieved by defining a get hook or not defining a hook at all.
  • A lastName property that is at least publicly settable. This can be achieved by defining a property which has a set hook or not defining a hook at all. But if the class is read-only then the property must have a set hook.

We could then create a User class that extends the Model class:

class User extends Model
{
    public string $fullName;

    public string $firstName {
        set(string $name) => ucfirst($name);
    }

    public string $lastName;

    public function __construct(
        string $firstName,
        string $lastName,
    ) {
        $this->firstName = $firstName;
        $this->lastName = $lastName;
    }
}

Conclusion

Hopefully, this article should have given you an insight into how PHP 8.4 property hooks work and how you might be able to use them in your PHP projects.

I wouldn't worry too much if this feature seems a little confusing at first. When I first saw it, I was a little confused too (especially with how they work with interfaces and abstract classes). But once you start tinkering with them, you'll soon get the hang of it.

I'm excited to see how this feature will be used in the wild and I'm looking forward to using it in my projects when PHP 8.4 is released.

If you enjoyed reading this post, you might be interested in checking out my 220 page ebook "Battle Ready Laravel" which covers similar topics in more depth.

Or, you might want to check out my other 440 page ebook "Consuming APIs in Laravel" which teaches you how to use Laravel to consume APIs from other services.

If you're interested in getting updated each time I publish a new post, feel free to sign up for my newsletter.

Keep on building awesome stuff! ?

版本聲明 本文轉載於:https://dev.to/ashallendesign/php-84-property-hooks-4nbd?1如有侵犯,請聯絡[email protected]刪除
最新教學 更多>
  • 如何在 Django 中記錄所有 SQL 查詢?
    如何在 Django 中記錄所有 SQL 查詢?
    如何在 Django 中記錄 SQL 查詢記錄 Django 應用程式執行的所有 SQL 查詢有利於偵錯和效能分析。本文提供了有關如何有效實現此目標的逐步指南。 配置要記錄所有SQL 查詢,包括管理網站產生的查詢,請將以下程式碼片段整合到settings.py 檔案中的LOGGING 欄位:LOGG...
    程式設計 發佈於2024-11-06
  • JavaScript 是同步還是異步,是單執行緒還是多執行緒? JavaScript程式碼是如何執行的?
    JavaScript 是同步還是異步,是單執行緒還是多執行緒? JavaScript程式碼是如何執行的?
    JavaScript 是一種同步、單執行緒語言,一次只能執行一個指令。僅噹噹前行執行完畢後,才會移至下一行。但是,JavaScript 可以使用事件循環、Promises、Async/Await 和回呼佇列執行非同步操作(JavaScript 預設是同步的)。 JavaScript程式碼是如何執行...
    程式設計 發佈於2024-11-06
  • 如何從 PHP 中的物件數組中提取一列屬性?
    如何從 PHP 中的物件數組中提取一列屬性?
    PHP:從物件數組中高效提取一列屬性許多程式設計場景都涉及使用物件數組,其中每個物件可能有多個屬性。有時,需要從每個物件中提取特定屬性以形成單獨的陣列。在 PHP 中,在不借助循環或外部函數的情況下用一行程式碼實現此目標可能很棘手。 一個可能的方法是利用 array_walk() 函數和 creat...
    程式設計 發佈於2024-11-06
  • 建構 PHP Web 專案的最佳實踐
    建構 PHP Web 專案的最佳實踐
    規劃新的 PHP Web 專案時,考慮技術和策略方面以確保成功非常重要。以下是一些規則來引導您完成整個過程: 1. 定義明確的目標和要求 為什麼重要:清楚了解專案目標有助於避免範圍蔓延並與利害關係人設定期望。 行動: 建立具有特定功能的專案大綱。 確定核心特徵和潛在的發展階段。 ...
    程式設計 發佈於2024-11-06
  • 如何在不使用巢狀查詢的情況下從 MySQL 中的查詢結果指派使用者變數?
    如何在不使用巢狀查詢的情況下從 MySQL 中的查詢結果指派使用者變數?
    MySQL 中根據查詢結果分配使用者變數背景和目標根據查詢結果分配使用者定義的變數可以增強資料庫操作能力。本文探討了在 MySQL 中實現此目的的方法,而無需借助嵌套查詢。 使用者變數賦值語法與流行的看法相反,使用者變數賦值可以直接整合到查詢中。 SET 語句的賦值運算子是= 或:=。但是,:= 必...
    程式設計 發佈於2024-11-06
  • 如何使用 array_column() 函數從 PHP 中的物件陣列中提取 Cat ID?
    如何使用 array_column() 函數從 PHP 中的物件陣列中提取 Cat ID?
    從PHP 中的物件陣列中提取貓ID處理物件陣列(例如貓物件陣列)時,提取特定屬性通常可以成為必要的任務。在這種特殊情況下,我們的目標是將每個 cat 物件的 id 屬性提取到一個新數組中。 正如您的問題中所建議的,一種方法涉及使用 array_walk() 和 create_function 。雖然...
    程式設計 發佈於2024-11-06
  • 實用指南 - 遷移到 Next.js App Router
    實用指南 - 遷移到 Next.js App Router
    隨著 Next.js App Router 的發布,許多開發者都渴望遷移他們現有的專案。在這篇文章中,我將分享我將專案遷移到 Next.js App Router 的經驗,包括主要挑戰、變更以及如何使流程更加順利。 這是一種增量方法,您可以同時使用頁面路由器和應用程式路由器。 為...
    程式設計 發佈於2024-11-06
  • 何時以及為何應調整 @Transactional 中的預設隔離和傳播參數?
    何時以及為何應調整 @Transactional 中的預設隔離和傳播參數?
    @Transactional中的隔離和傳播參數在Spring的@Transactional註解中,兩個關鍵參數定義了資料庫事務的行為:隔離和傳播。本文探討了何時以及為何應考慮調整其預設值。 傳播傳播定義了事務如何相互關聯。常見選項包括:REQUIRED: 在現有交易中執行程式碼,如果不存在則建立一個...
    程式設計 發佈於2024-11-06
  • OpenAPI 修剪器 Python 工具
    OpenAPI 修剪器 Python 工具
    使用 OpenAPI Trimmer 簡化您的 OpenAPI 文件 管理大型 OpenAPI 檔案可能會很麻煩,尤其是當您只需要一小部分 API 來執行特定任務時。這就是 OpenAPI Trimmer 派上用場的地方。它是一個輕量級工具,旨在精簡您的 OpenAPI 文件,使其...
    程式設計 發佈於2024-11-06
  • PHP:揭示動態網站背後的秘密
    PHP:揭示動態網站背後的秘密
    PHP(超文本預處理器)是一種伺服器端程式語言,廣泛用於建立動態和互動式網站。它以其簡單語法、動態內容生成能力、伺服器端處理和快速開發能力而著稱,並受到大多數網站託管服務商的支援。 PHP:揭秘動態網站背後的秘方PHP(超文本預處理器)是伺服器端程式語言,以其用於創建動態和互動式網站而聞名。它廣泛應...
    程式設計 發佈於2024-11-06
  • JavaScript 中的變數命名最佳實踐,實現簡潔、可維護的程式碼
    JavaScript 中的變數命名最佳實踐,實現簡潔、可維護的程式碼
    簡介:增強程式碼清晰度和維護 編寫乾淨、易於理解和可維護的程式碼對於任何 JavaScript 開發人員來說都是至關重要的。實現這一目標的一個關鍵方面是透過有效的變數命名。命名良好的變數不僅使您的程式碼更易於閱讀,而且更易於理解和維護。在本指南中,我們將探討如何選擇具有描述性且有意義的變數名稱,以顯...
    程式設計 發佈於2024-11-06
  • 揭示 Spring AOP 的內部運作原理
    揭示 Spring AOP 的內部運作原理
    在这篇文章中,我们将揭开 Spring 中面向方面编程(AOP)的内部机制的神秘面纱。重点将放在理解 AOP 如何实现日志记录等功能,这些功能通常被认为是一种“魔法”。通过浏览核心 Java 实现,我们将看到它是如何与 Java 的反射、代理模式和注释相关的,而不是任何真正神奇的东西。 ...
    程式設計 發佈於2024-11-06
  • JavaScript ESelease 筆記:釋放現代 JavaScript 的力量
    JavaScript ESelease 筆記:釋放現代 JavaScript 的力量
    JavaScript ES6,正式名稱為 ECMAScript 2015,引入了重大增強功能和新功能,改變了開發人員編寫 JavaScript 的方式。以下是定義 ES6 的前 20 個功能,它們使 JavaScript 程式設計變得更有效率和愉快。 JavaScript ES6 ...
    程式設計 發佈於2024-11-06
  • 了解 Javascript 中的 POST 請求
    了解 Javascript 中的 POST 請求
    function newPlayer(newForm) { fetch("http://localhost:3000/Players", { method: "POST", headers: { 'Content-Type': 'application...
    程式設計 發佈於2024-11-06
  • 如何使用 Savitzky-Golay 濾波平滑雜訊曲線?
    如何使用 Savitzky-Golay 濾波平滑雜訊曲線?
    雜訊資料的平滑曲線:探討Savitzky-Golay 濾波在分析資料集的過程中,平滑雜訊曲線的挑戰出現在提高清晰度並揭示潛在模式。對於此任務,特別有效的方法是 Savitzky-Golay 濾波器。 Savitzky-Golay 濾波器在資料可以透過多項式函數進行局部近似的假設下運作。它利用最小二乘...
    程式設計 發佈於2024-11-06

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3