How to add a new method to a php object on the fly?

后端 未结 9 2129
星月不相逢
星月不相逢 2020-11-28 04:03

How do I add a new method to an object \"on the fly\"?

$me= new stdClass;
$me->doSomething=function ()
 {
    echo \'I\\\'ve done something\';
 };
$me->         


        
相关标签:
9条回答
  • 2020-11-28 04:15

    Without the __call solution, you can use bindTo (PHP >= 5.4), to call the method with $this bound to $me like this:

    call_user_func($me->doSomething->bindTo($me, null)); 
    

    The complete script could look like this:

    $me = new stdClass;
    // Property for proving that the method has access to the above object:
    $me->message = "I\'ve done something"; 
    $me->doSomething = function () {
        echo $this->message;
    };
    call_user_func($me->doSomething->bindTo($me)); // "I've done something"
    

    Alternatively, you could assign the bound function to a variable, and then call it without call_user_func:

    $f = $me->doSomething->bindTo($me);
    $f(); 
    
    0 讨论(0)
  • 2020-11-28 04:16

    Update: The approach shown here has a major shortcoming: The new function is not a fully qualified member of the class; $this is not present in the method when invoked this way. This means that you would have to pass the object to the function as a parameter if you want to work with data or functions from the object instance! Also, you will not be able to access private or protected members of the class from these functions.

    Good question and clever idea using the new anonymous functions!

    Interestingly, this works: Replace

    $me->doSomething();    // Doesn't work
    

    by call_user_func on the function itself:

    call_user_func($me->doSomething);    // Works!
    

    what doesn't work is the "right" way:

    call_user_func(array($me, "doSomething"));   // Doesn't work
    

    if called that way, PHP requires the method to be declared in the class definition.

    Is this a private / public / protected visibility issue?

    Update: Nope. It's impossible to call the function the normal way even from within the class, so this is not a visibility issue. Passing the actual function to call_user_func() is the only way I can seem to make this work.

    0 讨论(0)
  • 2020-11-28 04:20

    you can also save functions in an array:

    <?php
    class Foo
    {
        private $arrayFuncs=array();// array of functions
        //
        public function addFunc($name,$function){
            $this->arrayFuncs[$name] = $function;
        }
        //
        public function callFunc($namefunc,$params=false){
            if(!isset($this->arrayFuncs[$namefunc])){
                return 'no function exist';
            }
            if(is_callable($this->arrayFuncs[$namefunc])){
                return call_user_func($this->arrayFuncs[$namefunc],$params);
            }
        }
    
    }
    $foo = new Foo();
    
    //Save function on array variable with params
    $foo->addFunc('my_function_call',function($params){
        return array('data1'=>$params['num1'],'data2'=>'qwerty','action'=>'add');
    });
    //Save function on array variable
    $foo->addFunc('my_function_call2',function(){
        return 'a simple function';
    });
    //call func 1
    $data = $foo->callFunc('my_function_call',array('num1'=>1224343545));
    var_dump($data);
    //call func 2
    $data = $foo->callFunc('my_function_call2');
    var_dump($data);
    ?>
    
    0 讨论(0)
  • 2020-11-28 04:23

    There's a similar post on stackoverflow that clears out that this is only achievable through the implementation of certain design patterns.

    The only other way is through the use of classkit, an experimental php extension. (also in the post)

    Yes it is possible to add a method to a PHP class after it is defined. You want to use classkit, which is an "experimental" extension. It appears that this extension isn't enabled by default however, so it depends on if you can compile a custom PHP binary or load PHP DLLs if on windows (for instance Dreamhost does allow custom PHP binaries, and they're pretty easy to setup).

    0 讨论(0)
  • 2020-11-28 04:24

    Using simply __call in order to allow adding new methods at runtime has the major drawback that those methods cannot use the $this instance reference. Everything work great, till the added methods don't use $this in the code.

    class AnObj extends stdClass
    {
        public function __call($closure, $args)
        {
            return call_user_func_array($this->{$closure}, $args);
        }
     }
     $a=new AnObj();
     $a->color = "red";
     $a->sayhello = function(){ echo "hello!";};
     $a->printmycolor = function(){ echo $this->color;};
     $a->sayhello();//output: "hello!"
     $a->printmycolor();//ERROR: Undefined variable $this
    

    In order to solve this problem you can rewrite the pattern in this way

    class AnObj extends stdClass
    {
        public function __call($closure, $args)
        {
            return call_user_func_array($this->{$closure}->bindTo($this),$args);
        }
    
        public function __toString()
        {
            return call_user_func($this->{"__toString"}->bindTo($this));
        }
    }
    

    In this way you can add new methods that can use the instance reference

    $a=new AnObj();
    $a->color="red";
    $a->sayhello = function(){ echo "hello!";};
    $a->printmycolor = function(){ echo $this->color;};
    $a->sayhello();//output: "hello!"
    $a->printmycolor();//output: "red"
    
    0 讨论(0)
  • 2020-11-28 04:26

    To see how to do this with eval, you can take a look at my PHP micro-framework, Halcyon, which is available on github. It's small enough that you should be able to figure it out without any problems - concentrate on the HalcyonClassMunger class.

    0 讨论(0)
提交回复
热议问题