Refactoring to property promotion In this refactoring we show the power of propery promotion, this can save you many lines of code https://www.php.net/manual/en/language.oop5.decon.php
requires: php 8.0.0

As of PHP 8.0.0, constructor parameters may also be promoted to correspond to an object property. It is very common for constructor parameters to be assigned to a property in the constructor but otherwise not operated upon. Constructor promotion provides a short-hand for that use case. The example above could be rewritten as the following.

Original code



        
        class User
        {
            public string $name;

            public string $email;

            public string $password;

            public function __construct(
                string $name,
                string $email,
                string $password
            ) {
                $this->name     = $name;
                $this->email    = $email;
                $this->password = $password;
            }
        }
       

        

Refactored code



        
        class User
        {
            public function __construct(
                public string $name,
                public string $email,
                public string $password,
        ) {}
        }
       

        

Want to learn more from LLoadout ?

LLoadout is your loadout for Laravel, helping you kickstart your development process. Besides this refactoring site we also make some cool packages and video tutorials !

Go to LLoadout on github