PHP 8. Todas las novedades de esta nueva versión (I)

Un repaso a fondo de las novedades que tendremos disponibles a partir del 26 de noviembre para trabajar con PHP.

The match expression

$count = 1;
$message = '';
switch ($count) {
case 0:
$message = 'No books';
break;
case 1:
$message = 'One book';
break;
case 2:
case 3:
$message = 'Some books';
break;
default:
$message = 'So many books';
break;
}
$count = 1;
$message = '';
$message = match($count) {
0 => 'No books',
1 => 'One book',
2,3 => 'Some books',
default => 'So many books'
};
$count = '1';
$message = '';
$message = match($count) {
0 => 'No books',
1 => 'One book',
2,3 => 'Some books',
default => 'So many books'
};
print_r($message); // 'So many books'
$count = 4;
$message = '';
try {
$message = match($count) {
0 => 'No books',
1 => 'One book',
2,3 => 'Some books'
};
} catch (UnhandledMatchError $exception) {
// handle case
}
$count = '1';
$message = '';
$message = match($count) {
0 => 'No books',
1 => 'One book',
2,3 => 'Some books',
default => throw new Exception('Unknown count')
};
print_r($message); // 'So many books'

Promoted Properties

<?php

class Article
{
private string $title;
public string $excerpt;

public function __construct(
string $title = 'Empty title',
string $excerpt = 'Empty excerpt'
) {
$this->title = $title;
$this->excerpt = $excerpt;
}
}
<?php

class Article
{
public function __construct(
public string $title = 'Empty title',
private string $excerpt = 'Empty excerpt'
) {}

}

Union Types

<?phpdeclare(strict_types=1);function foo(string $var) {
print_r($var);
}
foo('Hola mundo');foo(1); // Uncaught TypeError
<?phpdeclare(strict_types=1);function foo(string|int $var) {
print_r($var);
}
<?phpdeclare(strict_types=1);function foo(Dog|Cat $animal) {
print_r(get_class($animal));
}

Union types como valor de retorno

<?phpdeclare(strict_types=1);function foo(string|int $var): string|int {
// do something
return $var;
}

Null y Union Types

<?phpdeclare(strict_types=1);// ❌function foo(?string|int $var): string|int {
// do something
return $var;
}
<?phpdeclare(strict_types=1);// ✅function foo(string|int|null $var): string|int {
// do something
return $var;
}

Null Safe Operator

class Author
{
private string $name;
public function getName(): string
{
return $this->name;
}
}
class Article
{
private $author;
public function getAuthor():
{
return $this->author;
}
}
$article = new Article();// ❌
$authorName = $article->getAuthor()->getName();
$article = new Article();// ✅
$authorName = $article->getAuthor()?->getName();
class Author
{
private string $name;
public function getName(): string
{
return $this->name;
}
}
class Article
{
public ?Author $author;
}
$article = new Article();// ✅
$authorName = $article?->author->getName();

Named Arguments

function log(
string $message,
string $level = 'INFO',
string $date = 'now',
string $prefix = '',
bool $sendByEmail = false
) {
// do something
}
log('Some message', 'INFO', 'now', '', true);
//  👏log('Some message', sendByEmail: true);
log('Some message', sendByEmail: true, prefix: 'PHP8');
class Developer
{
public function __construct(
public string $name,
public string $company,
public string $language
) {}
}
$json = [
'name' => 'Gerardo',
'company' => 'Latte and Code',
'language' => 'PHP'
];
$developer = new Developer(...$json);
class Developer
{
public function __construct(
public string $name,
public string $company,
public string $language
) {}
}
$json = [
'name' => 'Gerardo',
'company' => 'Latte and Code',
'language' => 'PHP',
'age' => 32
];
$developer = new Developer(...$json); // ❌ Unknown parameter $age

Object Classnames

namespace App;class Developer
{
public function __construct(
public string $name,
public string $company,
public string $language
) {}
}
$dev = new Developer('Gerardo', 'LaC', 'PHP');get_class($dev); // App\Developer
$dev::class; // App\Developer

Continuará…

Vídeo

¿Quieres recibir más artículos como este?

Written by

Entre paseo y paseo con Simba desarrollo en Symfony y React

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store