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

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

Image for post
Image for post
Photo by Ben on Unsplash

Attributes

<?phpnamespace App\Attribute;use Attribute;#[Attribute]
class FooAttribute
{
public function __construct(
public string $foo
) {}
}
<?phpnamespace App\Service;#[FooAttribute('some value')]
class MyService
{
}
<?phpuse App\Service\MyService;$reflectionClass = new ReflectionClass(MyService::class);$attributes = $reflectionClass->getAttributes();
<?phpuse App\Service\MyService;$reflectionClass = new ReflectionClass(MyService::class);$attributes = $reflectionClass->getAttributes();$firstAttribute = $attributes[0];var_dump($attributes[0]->newInstance());
App\Attribute\FooAttribute
$foo -> "some value" (string)
<?phpuse App\Service\MyService;$reflectionClass = new ReflectionClass(MyService::class);$attributes = $reflectionClass->getAttributes();$firstAttribute = $attributes[0];var_dump($attributes[0]->newInstance()->foo); // some value
<?phpnamespace App\Service;use App\Attribute\{FooAttribute, BarAttribute};#[FooAttribute('some value'), BarAttribute('other value')]
class MyService
{
}

¿Dónde puedo usar los atributos?

<?phpnamespace App\Attribute;use Attribute;#[Attribute::TARGET_METHOD]
class FooAttribute
{
public function __construct(
public string $foo
} {}
}
<?phpnamespace App\Attribute;use Attribute;#[Attribute::TARGET_PROPERTY]
class FooAttribute
{
public function __construct(
public string $foo
} {}
}

¿Y para qué sirven?

use Symfony\Component\Routing\Annotation\Route;

class SomeController
{
#[Route('/path', name: 'action')]
public function someAction()
{
// ...
}
}

Mejoras en las excepciones

try {
throw new \Exception();
} catch (\Exception $exception) {
print_r('An error ocurred');
}
try {
throw new \Exception();
} catch (\Exception) {
print_r('An error ocurred');
}

“Trailing commas” en la definición de funciones

function foo(int $arg1, int $arg2,) {
}
$numbers = [
1,
2,
3,
];
$numbers = [
1,
2,
3,
4,
];
  $numbers = [
1,
2,
3,
+ 4,
];

Nuevas funciones para trabajar con strings

str_contains

$included = str_contains('hola mundo', ' mundo'); // true$included = str_contains('hola mundo', 'adiós); // false
$included = str_starts_with('hola mundo', 'hola '); // true$included = str_starts_with('hola mundo', 'mundo)'; // false

str_ends_with

$included = str_ends_with('hola mundo', 'mundo'); // true$included = str_ends_with('hola mundo', 'hola)'; // false

La interfaz Stringable

class Foo
{
public function __toString(): string
{
return 'foo class';
}
}
function log(string|Stringable $stringable) 
{
print_r (string)$stringable;
}
log(new Foo()); // 'foo class'

WeakMaps

$weakMap = new WeakMap();class Foo {};$foo = new Foo();
$otherFoo = new Foo();
$weakMap[$foo] = 'foo';
$weakMap[$otherFoo] = 'other foo';
print_r($weakMap[$otherFoo]); // 'other foo'print_r(count($weakMap)); // 2
unset($foo);
print_r(count($weakMap)); // 1

Conclusiones

Vídeo resumen

¿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