Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm using PHPs create_function($args, $code) function to dynamically load a function definition from a database.

The way I'm attempting to implement it is as follows:

I have a class MyClass which has an instance variable myFunction. The constructor populates that instance variable with the result of a call to create_function. I'm hoping to dynamically create a function for the specific object (once instantiated) of this class, that can be called as $object->myFunction(arg1, arg2);

So my class looks like:

class MyClass {

     public $myFunction = '';

     public function __construct() {
         $this->myFunction = //return function body from DB call.
     }


}

I'm then trying to call this dynamic function from elsewhere in my program on the instantiated "MyClass" object by doing something like...

$object = new MyClass();
$object->myFunction(args..); 

However I keep getting errors such as:

MyClass and its behaviors do not have a method or closure named myFunction.

When I run var_dump($object->myFunction) I get back "lambda_xx", which is a good sign meaning create_function is at least working.


Interesting Update on Works vs. Doesn't Work cases

It turns out that in my "other file" where I am doing the following:

   $pm = Yii::app()->user->postMatching; //This is a PostMatching object made elsewhere
    $c = $pm->findRelated;

    foreach ($posts as $post) {

        var_dump($c);

        $postIds = $c($post, $limit);

        //post to related mapping
        $specificRelatedPostIds[$post->postId] = $postIds;

    }
    exit; // exiting for testing

This doesn't work, but if instead of pulling the object $pm from Yii::app()->user->postMatching I just create a new one:

$pm = new PostMatching();
$c = $pm->findRelated; //the anon function instance variable

$c(); // THIS WORKS NOW!

So naturally I var_dumped $pm and $c in both the "newly created" case and the case where I get it from Yii::app()->user->postMatching, and they are identical. The only thing that is different is the name of the anonymous function (as expected).

Does anyone have any idea why this might be the case? In both cases $pm IS an instantiated PostMatching object with that instance variable, I'm just unable to use the syntax to invoke it!


Just updated the above with newly discovered "Twists", thanks guys!

share|improve this question

3 Answers

up vote 2 down vote accepted

Maybe something along these lines can be useful:

class MyClass {

     private $myFunction = '';

     public function __construct() {
         $this->myFunction = //return function body from DB call.
     }

     public function myFunction() {
         $args = func_get_args();
         return call_user_func_array($this->myFunction, $args);
     }

}
share|improve this answer
 
I get the error "call_user_func_array() expects parameter 1 to be a valid callback, function '' not found or invalid function name." when trying the above. If I var_dump($this->myFunction) I get "lambda_xx", so its not like its empty.... puzzled –  csjohn Oct 28 '11 at 4:00
 
Puzzled indeed. –  deceze Oct 28 '11 at 4:05
 
Try call_user_func_array(array($this,myFunction), $args) –  Levi Morrison Oct 28 '11 at 5:04
 
@Levi Since $this->myFunction is just a string denoting the name of a regular global function, that shouldn't be necessary. :-? –  deceze Oct 28 '11 at 5:18
 
The value of $this->myFunction may be regular and global, but to access it you still need to use the array syntax, I believe. –  Levi Morrison Oct 28 '11 at 14:41
show 1 more comments

That's due to parsing-related troubles that PHP has. This version should work:

$object = new MyClass();
$method = $object->myFunction;
$method(args..); 

See it in action.

share|improve this answer
 
I tried that syntax, but the call itself still won't fire. If I var_dump ($method) in this case, I get "lambda_xx", but if I call $method(arg1, arg2) it errors. Is $method in this case referring specifically to the $object's method?.. or would I need something like $object->$method (also tried and failed). Note: I'm making these calls to the anon function in a loop, but $method itself is instantiated prior to the loop. –  csjohn Oct 28 '11 at 3:42
 
@csjohn: That's not possible unless the working example is different code than yours. In which case, please post your real code. –  Jon Oct 28 '11 at 3:45
 
I added real code relating to this issue in the question above. Thanks! –  csjohn Oct 28 '11 at 3:56
 
@csjohn: OK, saw it and have to ask: why are you exposing a property instead of a method?!? Why not function findRelatedPosts($post, $limit) { ... }? –  Jon Oct 28 '11 at 14:33
 
At this point im just trying to get it to work. Once I get passed the barrier I'll write a public method to wrap the dynamic instance variable anonymous method and call like that. –  csjohn Oct 28 '11 at 22:47
show 2 more comments

You can call the method like this:

call_user_func($object->myFunction, args..);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.