Foros del Web » Programando para Internet » PHP »

[SOLUCIONADO] PHP OO Llamar metodo con variable

Estas en el tema de Llamar metodo con variable en el foro de PHP en Foros del Web. Hola que tal necesitaria saber de que forma podria hacer esto @import url("http://static.forosdelweb.com/clientscript/vbulletin_css/geshi.css"); Código PHP: Ver original class blabla {       public function ...
  #1 (permalink)  
Antiguo 26/07/2013, 21:44
 
Fecha de Ingreso: septiembre-2012
Ubicación: Buenos aires
Mensajes: 110
Antigüedad: 11 años, 6 meses
Puntos: 9
Pregunta Llamar metodo con variable

Hola que tal necesitaria saber de que forma podria hacer esto


Código PHP:
Ver original
  1. class blabla{
  2.  
  3.     public function metodo1($method, $param){}
  4.  
  5.         public function metodo2(){
  6.  
  7.             $var = 'metodo1';
  8.  
  9.             if( isset( $this->$var() ) ){
  10.  
  11.                 $this->$var($param);
  12.  
  13.             }
  14.  
  15.         }
  16.  
  17.     }
  18. }
  #2 (permalink)  
Antiguo 27/07/2013, 01:09
 
Fecha de Ingreso: agosto-2010
Mensajes: 24
Antigüedad: 13 años, 8 meses
Puntos: 2
Respuesta: Llamar metodo con variable

Creo que deberías ser mas explicito con lo que quieres, pero tal ves este ejemplo que hice te sirva.

Código:
class object
{
    function mi_metodo($string)
    {
        print "funciona ". $string;
    }
    function llamar_funcion($funcion, $paramtro)
    {
        if(in_array($funcion, get_class_methods($this)))
            $this->$funcion($paramtro);
    }
}
$obj = new object();
$obj->llamar_funcion("mi_metodo", " correctamente");
  #3 (permalink)  
Antiguo 27/07/2013, 10:22
Avatar de carlos_belisario
Colaborador
 
Fecha de Ingreso: abril-2010
Ubicación: Venezuela Maracay Aragua
Mensajes: 3.156
Antigüedad: 14 años
Puntos: 461
Respuesta: Llamar metodo con variable

Yo jugando un poco con reflection

Código PHP:
Ver original
  1. <?php
  2. class Test
  3. {
  4.     public function method1($name)
  5.     {
  6.         echo "hola $name";
  7.     }
  8.     public function method2()
  9.     {
  10.         try {
  11.             $method = 'method1';
  12.             $r = new ReflectionClass($this);
  13.             $r->getMethod($method)->invoke($this, 'carlos');
  14.         } catch(Exception $e) {
  15.             echo $e->getMessage();
  16.         }
  17.     }
  18. }
  19.  
  20. $a = new Test();
  21. $a->method2();

a lo mejor me regañan por ahí
__________________
aprende d tus errores e incrementa tu conocimientos
it's not a bug, it's an undocumented feature By @David
php the right way
  #4 (permalink)  
Antiguo 27/07/2013, 12:46
Avatar de dashtrash
Colaborador
 
Fecha de Ingreso: abril-2007
Ubicación: Ni en Sevilla,ni en Sanlúcar..qué más da..
Mensajes: 927
Antigüedad: 17 años
Puntos: 270
Respuesta: Llamar metodo con variable

Código PHP:
Ver original
  1. class object
  2. {
  3.     function mi_metodo($string)
  4.     {
  5.         print "funciona ". $string;
  6.     }
  7.     function __call($name,$arguments)
  8.     {
  9.           if(method_exists($this,$name))
  10.               call_user_func_array(array($this,$name),$arguments);
  11.      }
  12. }
  13. $obj = new object();
  14. $obj->mi_metodo("correctamente");
  #5 (permalink)  
Antiguo 27/07/2013, 16:20
 
Fecha de Ingreso: septiembre-2012
Ubicación: Buenos aires
Mensajes: 110
Antigüedad: 11 años, 6 meses
Puntos: 9
Respuesta: Llamar metodo con variable

Me tira:

Warning: call_user_func_array() expects parameter 2 to be array, string given in

En
Código PHP:
Ver original
  1. call_user_func_array(array($this, $obj[1]) , $param);

El $obj[1] tiene este valor = CropString.

El param = 'asdada',200

Código PHP:
Ver original
  1. ## ACLARACION
  2. ## BORRE MUCHO CODIGO POR ENDE DEJE EL QUE TENGO LA COMPLICACION
  3. ## EL ARRAY : $obj : TIENE:
  4. ## $obj[0] = 'string_lalalala';
  5. ## $obj[1] = 'CropString';
  6. ## $obj[2] = '200';
  7.  
  8. <?php
  9.  
  10.     # Paginador
  11.     class Paginator{
  12.        
  13.         #
  14.         public function edit($obj){
  15.        
  16.             # Si existe el metodo
  17.             if(method_exists($this,$obj[1])){
  18.            
  19.                 for($i=0;$i<count($reg);$i++){
  20.                    
  21.                     # Si existe en los registros el str indicado.
  22.                     if(isset($reg[$i][$obj[0]])){
  23.                        
  24.                         $param  = $reg[$i][$obj[0]].','.$obj[2];
  25.                         $reg[$i][$obj[0]] = call_user_func_array(array($this, $obj[1]) , $param);
  26.                    
  27.                     }
  28.                
  29.                 }
  30.            
  31.             }
  32.        
  33.         }
  34.        
  35.        
  36.        
  37.         # ----- Metodos Adicionales -----
  38.         private function CropString($string, $limit, $break='.', $pad='...'){
  39.        
  40.         }
  41.        
  42.        
  43.     }  
  44. ?>

Etiquetas: object, poo
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 05:53.