Crochets de propriété PHP

PHPz
Libérer: 2024-08-21 20:44:50
original
953 Les gens l'ont consulté

PHP  Property Hooks

Introduction

PHP 8.4 sortira en novembre 2024 et apportera une nouvelle fonctionnalité intéressante : les hooks de propriété.

Dans cet article, nous allons examiner ce que sont les hooks de propriété et comment vous pouvez les utiliser dans vos projets PHP 8.4.

En remarque, vous pourriez également être intéressé à consulter mon autre article qui vous montre les nouvelles fonctions de tableau qui sont ajoutées dans PHP 8.4.

Que sont les hooks de propriété PHP ?

Les hooks de propriété vous permettent de définir une logique getter et setter personnalisée pour les propriétés de classe sans avoir à écrire des méthodes getter et setter distinctes. Cela signifie que vous pouvez définir la logique directement dans la déclaration de propriété afin de pouvoir accéder directement à une propriété (comme $user->firstName) sans avoir à vous rappeler d'appeler une méthode (comme $user->getFirstName() et $user- >setFirstName()).

Vous pouvez consulter la RFC pour cette fonctionnalité sur https://wiki.php.net/rfc/property-hooks

Si vous êtes un développeur Laravel, en lisant cet article, vous remarquerez peut-être que les hooks ressemblent beaucoup aux accesseurs et aux mutateurs dans les modèles Laravel.

J'aime beaucoup l'apparence de la fonctionnalité de hooks de propriété et j'imagine que c'est quelque chose que j'utiliserai dans mes projets lorsque PHP 8.4 sera publié.

Pour comprendre le fonctionnement des hooks de propriété, examinons quelques exemples d'utilisation.

Le crochet "obtenir"

Vous pouvez définir un get hook qui sera appelé chaque fois que vous tenterez d'accéder à une propriété.

Par exemple, imaginez que vous ayez une simple classe User qui accepte un prénom et un nom dans le constructeur. Vous souhaiterez peut-être définir une propriété fullName qui concatène le prénom et le nom. Pour ce faire, vous pouvez définir un get hook pour la propriété fullName :

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
Copier après la connexion

Dans l'exemple ci-dessus, nous pouvons voir que nous avons défini un hook get pour la propriété fullName qui renvoie une valeur calculée en concaténant les propriétés firstName et lastName ensemble. Nous pouvons également nettoyer cela un peu plus en utilisant une syntaxe similaire aux fonctions fléchées :

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
Copier après la connexion

Compatibilité des types

Il est important de noter que la valeur renvoyée par le getter doit être compatible avec le type de la propriété.

Si les types stricts ne sont pas activés, la valeur sera jonglée avec le type de propriété. Par exemple, si vous renvoyez un entier à partir d'une propriété déclarée sous forme de chaîne, l'entier sera converti en chaîne :

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"
Copier après la connexion

Dans l'exemple ci-dessus, même si nous avons spécifié 123 comme entier à renvoyer, "123" est renvoyé sous forme de chaîne car la propriété est une chaîne.

Nous pouvons ajouter declare(strict_types=1); en haut du code comme ceci pour activer une vérification de type stricte :

declare(strict_types=1);

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

    public function __construct(
        public readonly string $firstName,
        public readonly string $lastName,
    ) {
        //
    }
}
Copier après la connexion

Maintenant, cela entraînerait une erreur car la valeur de retour est un entier, mais la propriété est une chaîne :

Fatal error: Uncaught TypeError: User::$fullName::get(): Return value must be of type string, int returned
Copier après la connexion

Le crochet "set"

Les hooks de propriété PHP 8.4 vous permettent également de définir un hook set. Ceci est appelé chaque fois que vous essayez de définir une propriété.

Vous pouvez choisir entre deux syntaxes distinctes pour le set hook :

  • Définir explicitement la valeur à définir sur la propriété
  • Utiliser une fonction flèche pour renvoyer la valeur à définir sur la propriété

Regardons ces deux approches. Nous imaginerons que nous souhaitons mettre en majuscules les premières lettres du prénom et du nom lorsqu'ils sont définis sur la classe User :

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
Copier après la connexion

Comme nous pouvons le voir dans l'exemple ci-dessus, nous avons défini un hook set pour la propriété firstName qui met en majuscule la première lettre du nom avant de la définir sur la propriété. Nous avons également défini un hook set pour la propriété lastName qui utilise une fonction de flèche pour renvoyer la valeur à définir sur la propriété.

Compatibilité des types

Si la propriété a une déclaration de type, alors son hook set doit également avoir un type compatible. L'exemple suivant renverra une erreur car le hook set pour firstName n'a pas de déclaration de type, mais la propriété elle-même a une déclaration de type 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;
    }
}
Copier après la connexion

Tenter d'exécuter le code ci-dessus entraînerait l'erreur suivante :

Fatal error: Type of parameter $name of hook User::$firstName::set must be compatible with property type
Copier après la connexion

Utiliser les hooks "get" et "set" ensemble

Vous n'êtes pas limité à utiliser les crochets get et set séparément. Vous pouvez les utiliser ensemble dans la même propriété.

Prenons un exemple simple. Nous imaginerons que nous avons une propriété fullName sur notre classe User. Lorsque nous définissons la propriété, nous diviserons le nom complet en prénom et nom. Je sais que c'est une approche naïve et qu'il existe de bien meilleures solutions, mais c'est uniquement à titre d'exemple pour mettre en évidence les propriétés accrochées.

Le code peut ressembler à ceci :

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
Copier après la connexion

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;
    }
}
Copier après la connexion

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);
        }
    ) {
        //
    }
}  
Copier après la connexion

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!
Copier après la connexion

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
Copier après la connexion

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!
    }
}
Copier après la connexion

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
Copier après la connexion

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;
    }
}
Copier après la connexion

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;
    }
}
Copier après la connexion

The above code would throw the following error:

Fatal error: Hooked properties cannot be readonly
Copier après la connexion

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;
    }
}
Copier après la connexion

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
Copier après la connexion

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; }
}
Copier après la connexion

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;
    }
}
Copier après la connexion

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; }
}
Copier après la connexion

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)
Copier après la connexion

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; }
}
Copier après la connexion

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;
    }
}
Copier après la connexion

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! ?

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

source:dev.to
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!