Instead of "creating" functions, you can use the magic method __call(), so that when you call a "non-existent" function, you can handle it and do the right action.
Something like this:
class MyClass{ private $array = array('one', 'two', 'three'); function __call($func, $params){ if(in_array($func, $this->array)){ return 'Test'.$func; } } }Then you can call:
$a = new MyClass; $a->one(); // Testone $a->four(); // nullDEMO: //ideone.com/73mSh
EDIT: If you are using PHP 5.3+, you actually can do what you are trying to do in your question!
class MyClass{ private $array = array('one', 'two', 'three'); function __construct(){ foreach ($this->array as $item) { $this->$item = function() use($item){ return 'Test'.$item; }; } } }This does work, except that you can't call $a->one() directly, you need to save it as a variable.
$a = new MyClass; $x = $a->one; $x() // TestoneDEMO: //codepad.viper-7.com/ayGsTu
7.12. Creating a Class Dynamically
Problem
You want to create a class, but you don’t know everything about it until your code is executed.
Solution
Use eval( ) with interpolated variables:
eval("class van extends $parent_class { function van() { \$this->$parent_class(); } };"); $mystery_machine = new van;Discussion
While it’s okay in PHP to use variable names to call functions or create objects, it’s not okay to define functions and classes in a similar manner:
$van( ); // okay $van = new $parent_class // okay function $van( ) {}; // bad class $parent_class {}; // badTrying to do either of the last two examples results in a parser error because PHP expects a string, and you supplied a variable.
So, if you want to make a class named $van and you don’t know beforehand what’s going to be stored in $van, you need to employ eval( ) to do your dirty work:
eval("class $van {};");There is a performance hit whenever you call eval( ), so high traffic sites should try to restructure their code to avoid this technique when possible. Also, if you’re defining your class based on input from users, be sure to escape any potentially dangerous characters.
A simple definition for methods is: Methods are functions encapsulated within a class. Now lets explain how to dynamically call methods.
Sometimes we need to call different methods as per our requirement and conditions. To do so, we can call any method dynamically.
To call a method dynamically means that we can assign method name to any variable and call the method using that variable, either in switch case of if else block or anywhere depending on our requirement. Below is the snippet and code explanation which demonstrate how to call a method dynamically.
Code to call a method dynamically
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <?php classDynamicCalling{ publicfunctioncaller($to_call){ echo'<h4>Calling "'. $to_call.'" method</h4>'; if(is_callable([$this,$to_call])){ $this->$to_call(); }else{ echo'<h3>Unable to call "'.$to_call.'" method: method missing</h3>'; } } publicfunctionmethodOne(){ echo'<h3>This is methodOne.</h3>'; } private functionmethodTwo(){ echo'<h3>This is methodTwo.</h3>'; } } echo'<center>'; $dcObject= newDynamicCalling(); $dcObject->caller('methodOne'); echo'<hr/>'; $dcObject->caller('methodAny'); echo'<hr/>'; $dcObject->caller('methodTwo'); ?> |
Code explanation:
Above is a class DynamicCalling which has three methods: caller(), methodOne(), methodTwo().
Purpose of the caller() method is to call other methods by passing their names as argument $to_call. If the method of the name same as the value of $to_call is callable, i.e. available and can be called, then it is called. This validity is checked by is_callable() function of core PHP in which passed $this as object of same class for method reference and $to_call as method name to check valid method of class DynamicCalling.
If you need to call method of any other class, simply replace the $this with object of that class and pass the method name.
Example to call other class method dynamically
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | <?php classDynamicCalling{ publicfunctioncaller($to_call){ echo'<h4>Calling "'. $to_call.'" method</h4>'; if(is_callable([$this,$to_call])){ $this->$to_call(); }else{ echo'<h3>Calling method in ThirdParty class.</h3>'; $tpObject= newThirdParty(); if(is_callable([$tpObject,$to_call])){ $tpObject->$to_call(); }else{ echo'<h3>"'.$to_call.'" method not found in ThirdParty class</h3>'; } } } publicfunctionmethodOne(){ echo '<h3>This is methodOne.</h3>'; } privatefunctionmethodTwo(){ echo'<h3>This is methodTwo.</h3>'; } } classThirdParty{ publicfunctionmethodAny(){ echo'<h3>This is methodAny of ThirdParty class.</h3>'; } } echo'<center>'; $dcObject=newDynamicCalling(); $dcObject->caller('methodOne'); echo'<hr/>'; $dcObject->caller('methodAny'); echo'<hr/>'; $dcObject->caller('methodTwo'); ?> |