Ver Mensaje Individual
  #7 (permalink)  
Antiguo 30/05/2014, 14:16
Avatar de Italico76
Italico76
 
Fecha de Ingreso: abril-2007
Mensajes: 3.303
Antigüedad: 17 años
Puntos: 292
Respuesta: Nuevos tipos de datos: Struct de C y Range

@lolainas : que te digo ? tu version funciona, es mas simple..... me gusta y mucho excepto porque la forma de utilizarla es mucho mas complicada, toca crear una clase y escribir un su constructor una llamada al constructor del padre Struct con un array tambien hecho a mano.


Como me destacaba por MP el compañero @hhs ... es importante ofrecer una "API simple", igualmente me tome el atrevimiento de "mejorar" un poco tu version y agregarle el alias de tipos:

Código PHP:
Ver original
  1. abstract class Struct
  2. {
  3.  
  4.     private $data;
  5.     private $definition;
  6.     private $_aliases = array();   
  7.  
  8.     function __construct(array $definition) {
  9.         $this->definition = $definition;
  10.         $this->alias();
  11.     }
  12.  
  13.     function __get($name) {
  14.         return $this->data[$name];
  15.     }
  16.  
  17.     function __set($name, $value)
  18.     {  
  19.         if (isset($this->definition[$name]))
  20.         {
  21.             $value_type = gettype($value);
  22.             $definition_type = $this->definition[$name];
  23.        
  24.             // alias
  25.             foreach ((array) $this->_aliases as $alias => $typo)
  26.                 if ($definition_type==$alias) $definition_type=$typo;
  27.                        
  28.             if (($value_type === $definition_type) || $value instanceof $definition_type)
  29.                 $this->data[$name] = $value;
  30.                
  31.             else
  32.                 throw new Exception("Trying to assign ({$definition_type}: {$name}) with {$value_type}");
  33.         }else
  34.             throw new Exception("Trying to access undefined variable {$name}");
  35.            
  36.     }      
  37.    
  38.     /* setter on-off */
  39.     function alias($flag=true)
  40.     {
  41.         $this->_aliases= $flag ? array('bool'=>'boolean','int'=>'integer','float'=>'double','str'=>'string') : null;
  42.            
  43.         return $this;  
  44.     }  
  45. }

Código PHP:
Ver original
  1. class Persona extends Struct {
  2.  
  3.     function __construct() {
  4.         parent::__construct([
  5.             'nombre'           => 'str',
  6.             'apellidos'        => 'str',
  7.             'hijos'            => 'int',
  8.             'fecha_nacimiento' => 'DateTime',
  9.             'telefonos'        => 'array'
  10.         ]);
  11.     }
  12.  
  13. }
  14.  
  15. $p = new Persona;
  16.  
  17. $p->nombre = 'Pablo';
  18. $p->apellidos = 'Bozzolo';
  19. $p->fecha_nacimiento = new DateTime('1980-10-01');
  20. $p->telefonos = ['435435435', '342343242'];
  21.  
  22. debug($p);
__________________
Salu2!

Última edición por Italico76; 30/05/2014 a las 16:32