Ver Mensaje Individual
  #17 (permalink)  
Antiguo 22/04/2010, 11:53
Avatar de GatorV
GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 10 meses
Puntos: 2135
Respuesta: Pierdo variables usando una clase dentro de otra

Código PHP:
Ver original
  1. <?php  
  2.  
  3. class Flickr {  
  4.  
  5.     private $apiKey = 'c933e0d66a6695d005d660392bc496f2';  
  6.  
  7.     public function img($id)  
  8.     {  
  9.         $url = 'http://www.flickr.com/services/rest/?api_key=' . $this->apiKey . '&format=php_serial&method=flickr.photos.getInfo&photo_id=' . $id;  
  10.  
  11.         $resultado = file_get_contents($url);  
  12.         $resultado = unserialize($resultado);  
  13.         $foto = $resultado[photo];  
  14.  
  15.         $foto_url = 'http://farm' . $foto[farm] . '.static.flickr.com/' . $foto[server] . '/' . $foto[id] . '_' . $foto[secret] . $tam . '.jpg';  
  16.  
  17.         $foto = '<a href="' . $foto[urls][url][0][_content] . '"><img src="' . $foto_url . '" alt="' . $foto[title][_content] . '" /></a>';  
  18.  
  19.         return $foto;  
  20.     }  
  21.  
  22. }  
  23.  
  24. $flickr = new Flickr();  
  25.  
  26.  
  27.  
  28. class BBcode {  
  29.  
  30.     function __construct(&$flickr)  
  31.     {  
  32.         $this->flickr =& $flickr;  
  33.     }  
  34.  
  35.     function procesar($texto){  
  36.  
  37.         $a = "/\[img\](.*?)\[\/img\]/is";  
  38.  
  39.         $texto = preg_replace_callback($a, array($this, 'process_img'), $matches);
  40.          
  41.         return $texto;  
  42.     }
  43.    
  44.     public function process_img($matches)
  45.     {
  46.         $img_id = $matches[1];
  47.         $this->flickr->img($img_id);
  48.     }
  49. }  
  50.  
  51. $texto  = 'Imagen 1:<br />';  
  52. $texto .= '[img]4398563145[/img]<br />';  
  53. $texto .= 'Imagen 2:<br />';  
  54. $texto .= '[img]4334520973[/img]';  
  55.  
  56. $bbcode = new BBcode($flickr);  
  57.  
  58. echo $bbcode->procesar($texto);