Why is there a function create_function()
if I could just create the function something() { ... }
. What is create_function(string $args, string $code);
really meant for?
For example should I want to echo
a specific value, written long hand:
function sayHi($name){
echo 'Hi,' . $name;
}
//using it like:
sayHi('Jacques Marais');
But then using the create_function()
method:
$sayHi = create_function('$name', 'echo \'Hi,\' . $name;');
//using it like:
$sayHi('Jacques Marais');
Anonymous functions, also known as closures , allow the creation of functions which have no specified name. They are most useful as the value of callable parameters, but they have many other uses. Anonymous functions are implemented using the Closure class.
The return keyword ends a function and, optionally, uses the result of an expression as the return value of the function. If return is used outside of a function, it stops PHP code in the file from running.
PHP Function Arguments An argument is just like a variable. Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma. The following example has a function with one argument ($fname).
Advantages of PHP FunctionReusability of Code: Unlike other programming languages, PHP Functions are specified only once and can be called multiple times. Less Code: It saves a lot of code because the logic doesn't have to be written several times. You can write the logic only once and reuse it by using functions.
Because of you can use it in many ways.. Even in an array!
$farr = array(
create_function('$x,$y', 'return "some trig: ".(sin($x) + $x*cos($y));'),
create_function('$x,$y', 'return "a hypotenuse: ".sqrt($x*$x + $y*$y);'),
create_function('$a,$b', $f1),
create_function('$a,$b', $f2),
create_function('$a,$b', $f3)
);
You're looking at just one example, but the use of this function is more complicated, you can use it in many different ways which will be easier then using the function()
.
Like example#3 on PHP.net
<?php
$av = array("the ", "a ", "that ", "this ");
array_walk($av, create_function('&$v,$k', '$v = $v . "mango";'));
print_r($av);
?>
The above example will output:
Array
(
[0] => the mango
[1] => a mango
[2] => that mango
[3] => this mango
)
create_function() is mainly from PHP 4, in new PHP (>=5) you should use anonymous functions. The difference can be a scope and garbage collecting, not sure. create_function() will have to evaluate the string, it can be less secure. I can see that function as deprecated, there are better ways even how to add dynamically a method to the class and you can use anonymous (lambda) functions in array as well.
It's come to php from functional programming I guess...
It's calling anonymous function. You can use it if you need pass function to the another function. For example usort function of php. And of course not only for this. You can use it in arrays or you can return the function from another function.
The goal is to create function dynamically. Not replacing manual declaration, otherwise I agree that would not be really useful.
So you can create functions with name passed as an argument, like you do with magic methods __get() on classes, or use it in loop to declare several similar functions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With