Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create runtime function which uses object context?

I am having a class and I have a dynamically created function (created via "create_function") but I cannot find a way to tell PHP that I want this function to be created for this class only (class function) and because of that the new function cannot access the object properties. Take a look at the following code:

class Test {
  private $var=1;

  function __construct() {
      call_user_func(create_function('', 'echo $this->var;'));
  }
}

new Test;

This throws error "Fatal error: Using $this when not in object context in D:\WWW\index.php(7) : runtime-created function on line 1"

like image 980
barakuda28 Avatar asked Jul 15 '26 05:07

barakuda28


1 Answers

You probably want runkit_method_add, not create_function.

like image 158
lanzz Avatar answered Jul 17 '26 17:07

lanzz