Ver Mensaje Individual
  #3 (permalink)  
Antiguo 19/05/2010, 13:29
Avatar de GatorV
GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 11 meses
Puntos: 2135
Respuesta: Seter en objetos

Siento que es de mala manera el dirigir el post a alguien.

Fuera de eso puedes aprovechar y usar method_exists para ir guardando los valores, por ejemplo:
Código PHP:
Ver original
  1. class Cliente
  2. {
  3.       private $_nombre;
  4.       private $_edad;
  5.  
  6.       public function setEdad($edad)
  7.       {
  8.              $this->_edad = $edad;
  9.              return $this;
  10.       }
  11.       public function getEdad()
  12.       {
  13.              return $this->_edad;
  14.       }
  15.  
  16.       public function setNombre($nombre)
  17.       {
  18.              $this->_nombre = $nombre;
  19.              return $this;
  20.       }
  21.       public function getNombre()
  22.       {
  23.              return $this->_nombre;
  24.       }
  25.  
  26.       public function fetchData($id)
  27.       {
  28.                $result = $this->db->fetch('SELECT nombre, edad FROM clientes WHERE id_cliente='.$id);
  29.                // suponiendo que db->fetch nos regresa un array asi:
  30.                // array( 'nombre' => 'fulano', 'edad' => 20)
  31.  
  32.                foreach ($result as $field => $value) {
  33.                        $method = 'set' . ucfirst($field);
  34.                        if (method_exists($this, $method) {
  35.                                 $this->$method($value);
  36.                        }
  37.                }
  38.       }
  39. }

Con eso ya podrías mandar llamar:
Código PHP:
Ver original
  1. $cliente = new Cliente();
  2. $cliente->fetchData(1);
  3. echo $cliente->getEdad();
  4. echo $cliente->getNombre();

Saludos.