Ver Mensaje Individual
  #2 (permalink)  
Antiguo 15/03/2013, 01:25
cachusan
 
Fecha de Ingreso: septiembre-2011
Mensajes: 219
Antigüedad: 12 años, 7 meses
Puntos: 31
Respuesta: Como logro esto en php [Class]

Se denomina encadenar metodos, podes buscarlo en google o tomar este codigo como referencia, no es funcional porque obviamente le faltan cosas para generar un thumb y algunas validaciones, pero te sirve para entender como encadenar. El secreto esta en "return $this;"

Y por si a alguien no le queda, es un mero ejemplo, no es algo funcional. Es para ilustrar la idea.

Código PHP:
Ver original
  1. <?php
  2.  
  3. Class Thumbs{
  4.  
  5.     private $path;
  6.     private $width = 50;
  7.     private $height = 50;
  8.  
  9.     /**
  10.      *
  11.      */
  12.     function __construct($path = null){
  13.         if(isset($path)){
  14.             $this->thumb = $path;
  15.         }
  16.     }
  17.  
  18.     /**
  19.      *
  20.      */
  21.     public function width($w = null){
  22.         $this->width = $w;
  23.         return $this;
  24.     }
  25.  
  26.     /**
  27.      *
  28.      */
  29.     public function height($h = null){
  30.         $this->height = $h;
  31.         return $this;
  32.     }
  33.  
  34.  
  35.     /**
  36.      *
  37.      */
  38.     public function generate($file){
  39.         $w = $this->width;
  40.         $h = $this->height;
  41.         $path = $this->path;
  42.         $file = $this->file;
  43.  
  44.         /*
  45.         * codigo para generar el thumb
  46.         * con las medidas generadas a
  47.         * travez de encadenamiento
  48.         */
  49.  
  50.         return $something;
  51.     }
  52.  
  53. }
  54.  
  55. ?>

Con una clase así, podes hacer lo siguiente:

Código PHP:
Ver original
  1. $thumb = new Thumbs('imagen.jpg'); // inicio la clase
  2. // reescribo todos los parametros
  3. $procesada = $thumb->width(150)->height(150)->generate('mi_nuevo_thumb.jpg');
  4. // o dejando los valores por defecto
  5. $procesada = $thumb->generate('mi_nuevo_thumb.jpg');

Espero te sirva como base para que lo deseas hacer.
Saludos.