Ver Mensaje Individual
  #1 (permalink)  
Antiguo 23/03/2014, 12:06
Avatar de NSD
NSD
Colaborador
 
Fecha de Ingreso: mayo-2012
Ubicación: Somewhere
Mensajes: 1.332
Antigüedad: 12 años, 9 meses
Puntos: 320
Encapsular referencia externa dentro de objeto PHP5

Código PHP:
Ver original
  1. <?php
  2.  class Encapulador
  3.  {
  4.     private $mysqli     = null;
  5.     private $stmt       = null;
  6.     private $oneRef     = 3;
  7.    
  8.     public function __construct()
  9.     {  
  10.         $this->mysqli = new mysqli('localhost', 'root', '', 'prueba'); 
  11.         $this->stmt = $this->mysqli->prepare("SELECT * FROM `items` WHERE `nro-i` = ?;");
  12.         $this->stmt->bind_param('i', $this->oneRef);
  13.     }
  14.    
  15.     public function setVal($val)
  16.     {
  17.         $this->oneRef = $val;
  18.     }
  19.    
  20.     public function execute()
  21.     {
  22.         $this->stmt->execute();
  23.         $registros = $this->stmt->get_result();
  24.         while($registro = $registros->fetch_array(MYSQLI_ASSOC))
  25.             var_dump($registro);
  26.     }
  27.  }
  28.  
  29.     $test = new Encapulador();
  30.    
  31.     $a = 2;
  32.     $test->setVal($a);
  33.     $test->execute();
  34.    
  35.     echo('<hr>');
  36.     $a = 5;
  37.     $test->setVal($a);
  38.     $test->execute();
  39.    
  40.     echo('<hr>');
  41.     $a = 13;
  42.     $test->setVal($a);
  43.     $test->execute();
  44.     echo('<hr>');

EDITO: YA LO SOLUCIONE
__________________
Maratón de desafíos PHP Junio - Agosto 2015 en FDW | Reglamento - Desafios

Última edición por NSD; 23/03/2014 a las 12:25 Razón: YA LO SOLUCIONE