0

Is it possible to create a method dynamically? For example: I have a method called "createMethod()". I would like to pass variables to this method describing how the dynamic method would look like (function description, params as an array or object). I have been trying to make something like that, bet I've got stuck at describing params and got mixed up with eval() function, that I used to form some variables.

Could someone give me a simple example how this dynamic method would look like? Maby there are something helpful already created in Zend or CodeIgniter frameworks?

Thanks!

Kęstutis
  • 55
  • 5
  • Yes, look at [`__call`](http://www.php.net/manual/en/language.oop5.overloading.php#object.call), which catches any calls to methods that do not exist. You can make a dynamic call using `$method = 'callme'; $this->$method($params)`. – halfer Nov 21 '13 at 23:56
  • Can you concretise the use case / context? – mario Nov 22 '13 at 00:00

2 Answers2

0

You can find a good solution at: php create class method at runtime

Is a mix of the magic method __call and closures.

And this a post about how extending objects with new methods at runtime

Community
  • 1
  • 1
mcuadros
  • 4,098
  • 3
  • 37
  • 44
0

php magic method __call]1 could be what youre looking for

ilpaijin
  • 3,645
  • 2
  • 23
  • 26