Instead of "creating" functions, you can use the magic method Show Something like this:
Then you can call:
DEMO: http://ideone.com/73mSh EDIT: If you are using PHP 5.3+, you actually can do what you are trying to do in your question!
This does work, except that you can't call
DEMO: http://codepad.viper-7.com/ayGsTu 7.12. Creating a Class DynamicallyProblemYou want to create a class, but you don’t know everything about it until your code is executed. SolutionUse 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 {}; // bad Trying 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 eval("class $van {};"); There
is a performance hit whenever you call 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
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
How to call method dynamically in PHP?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.
How to create function dynamically in PHP?If you're using PHP 5.3 you could use an anonymous function. <? php $functionName = "doStuff"; $$functionName = function($args) { // Do stuff }; $args = array(); $doStuff($args); ?> Technically, this doesn't define a function, but a closure.
How do you call a class method in PHP?How to call a method?. First, we create an object ( $example ) from the class Example.. Next, we call the method echo with -> (object operator) and () (parentheses). The parentheses contain the arguments as usual.. |