Ver Mensaje Individual
  #29 (permalink)  
Antiguo 08/05/2011, 13:47
Avatar de masterpuppet
masterpuppet
Software Craftsman
 
Fecha de Ingreso: enero-2008
Ubicación: Montevideo, Uruguay
Mensajes: 3.550
Antigüedad: 16 años, 3 meses
Puntos: 845
Respuesta: Clave numérica en objeto?

@metacortex, si es una colección, segui tratándolo como un array, y cuando desarrolles la clase "collection" le das soporte para acceder como array a través de ArrayAccess, podes hacer esto:

Código PHP:
Ver original
  1. //como array
  2. print_r($collection[101]);
  3.  
  4. //como objeto
  5. print_r($collection->get(101));

Una implementacion muy simple:

Item, clase básica para guardar en la colección, en tu caso seria Image o algo parecido.
Código PHP:
Ver original
  1. class Item
  2. {
  3.     /**
  4.      * @var string|integer
  5.      */
  6.     protected $_id;
  7.  
  8.     /**
  9.      * @var string
  10.      */
  11.     protected $_name;
  12.  
  13.     /**
  14.      * @param string|integer $id
  15.      * @param string $name
  16.      */
  17.     public function __construct($id, $name)
  18.     {
  19.         $this->_id   = (int)$id;
  20.         $this->_name = (string)$name;
  21.     }
  22.  
  23.     /**
  24.      * @return string|integer $_id
  25.      */
  26.     public function getId()
  27.     {
  28.         return $this->_id;
  29.     }
  30.  
  31.     /**
  32.      * @param string $_name
  33.      * @return Item provide fluent interface
  34.      */
  35.     public function setName($name)
  36.     {
  37.         $this->_name = (string)$name;
  38.         return $this;
  39.     }
  40.  
  41.     /**
  42.      * @return string $_name
  43.      */
  44.     public function getName()
  45.     {
  46.         return $this->_name;
  47.     }
  48.  
  49.     public function __toString()
  50.     {
  51.         return (string)$this->_id;
  52.     }
  53. }

Collection, esta clase deberia implementar Iteraror, SeekableIterator, ArrayAccess y Countable, pero para simplificar, en ejemplo utilizo IteratorAggregate.
Código PHP:
Ver original
  1. class Collection implements IteratorAggregate
  2. {
  3.     /**
  4.      * @var array
  5.      */
  6.     protected $_data = array();
  7.  
  8.     /**
  9.      * Implements IteratorAggregate
  10.      * @return ArrayIterator
  11.      */
  12.     public function getIterator()
  13.     {
  14.         return new ArrayIterator($this->_data);
  15.     }
  16.  
  17. }
  18.  
  19. class ItemCollection extends Collection
  20. {
  21.     /**
  22.      * @param Item $i
  23.      * @return Collection provide fluent interface
  24.      */
  25.     public function add(Item $i)
  26.     {
  27.         $this->_data[(string)$i] = $i;
  28.         return $this;
  29.     }
  30. }

Test:

Código PHP:
Ver original
  1. $collection = new ItemCollection();
  2. $i1 = new Item(101, 'Item 1');
  3. $i2 = new Item(201, 'Item 2');
  4.  
  5. $collection->add($i1)
  6.            ->add($i2);
  7.  
  8. $it = $collection->getIterator();
  9.  
  10. assert(count($it) === 2); //pass
  11. assert($it->offsetGet(101) === $it[101]); //pass
  12. assert($it->offsetGet(101) === $it[201]); //fail
  13.  
  14. foreach($it as $item){
  15.     echo $item->getId() . ' - ' . $item->getName() . PHP_EOL;
  16. }

Es algo sencillo de conseguir y te permite acceder a los elementos de la colleción de las dos formas como objeto o como array.

@abimaelrc, hay que tener cuidado con esa forma de implementar __set, porque tiene un efecto poco deseable, podes setear cualquier cosa, es como si las propiedades fueran públicas, por eso se setean en el array las keys y se comprueba que existan.


Saludos.
__________________
http://es.phptherightway.com/
thats us riders :)