Diferencia entre revisiones de «Constructor Property Promotion»

De Wiki Proyectos Beta
Ir a la navegación Ir a la búsqueda
([feat] Add information about PHP 8.0 - Constructor Property Promotion)
 
([feat] Add information about Constructor Property Promotion)
 
Línea 8: Línea 8:
         public string $email;
         public string $email;
         public int $edad;
         public int $edad;
 
 
         public function __construct (string $nombre, string $email, int $edad)
         public function __construct (string $nombre, string $email, int $edad)
         {
         {
Línea 18: Línea 18:


PHP 8 con el Constructor Property Promotion:
PHP 8 con el Constructor Property Promotion:
   <?php
   <?php
     // Prueba usando property PHP 8.
     // Prueba usando property PHP 8.

Revisión actual - 06:32 18 ago 2022

RFC EN

En versiones anteriores de PHP 8, al escribir una clase y asignar valores en las propiedades hacíamos de esta manera:

 <?php 
   class Empleado
   {
       public string $nombre;
       public string $email;
       public int $edad;
 
       public function __construct (string $nombre, string $email, int $edad)
       {
           $this->nombre = $nombre;
           $this->email = $email;
           $this->edad = $edad;
       }
   }

PHP 8 con el Constructor Property Promotion:

 <?php
   // Prueba usando property PHP 8.
   class Empleado
   {
       public function __construct (
         public string $nombre,
         public string $email,
         public int $edad
       ){}
    }