What's new in PHP 8.0

Named arguments

  • Specify only required parameters, skipping optional ones.
  • Arguments are order-independent and self-documented.
function printArguments($argument_1 = '', $argument_2 = '', $argument_3 = '')
{
    echo "Argument 1: " . $argument_1 . PHP_EOL;
    echo "Argument 2: " . $argument_2 . PHP_EOL;
    echo "Argument 3: " . $argument_3 . PHP_EOL;
}

printArguments();


printArguments(argument_1: 'Value 1', argument_2: 'Value 2', argument_3: 'Value 3');


printArguments(argument_3: 'Value 3');

 

Attributes 

Instead of PHPDoc annotations, you can now use structured metadata with PHP's native syntax.

in PHP 7:


class UsersController
{
    /**
     * @Route("/users/{id}", methods={"GET"})
     */
    public function get($id) { /* ... */ }
}

in PHP 8:


class UsersController
{
    #[Route("/users/{id}", methods: ["GET"])]
    public function get($id) { /* ... */ }
}

 

Constructor property promotion

Less boilerplate code to define and initialize properties.

in PHP 7:


class User {
    public string $name;
    public int $age;
    public string $country;

    public function __construct(
        string $name = 'John Doe',
        int $age = 0,
        string $country = 'USA'
    ) {
        $this->name = $name;
        $this->age = $age;
        $this->country = $country;
    }
}

in PHP 8:


class User {
    public function __construct(
        public string $name = 'John Doe',
        public int $age = 0,
        public string $country = 'USA',
    ) {}
}