Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there a function create_function()? in PHP

Tags:

function

php

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');
like image 813
LightningBoltϟ Avatar asked Dec 05 '13 08:12

LightningBoltϟ


People also ask

What is an anonymous function in PHP?

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.

How is it possible to return a value from a function in PHP?

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.

What is an argument in PHP?

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).

What is the advantage of using function in PHP?

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.


4 Answers

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
)
like image 93
Joran Den Houting Avatar answered Oct 18 '22 23:10

Joran Den Houting


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.

like image 30
martin.malek Avatar answered Oct 18 '22 23:10

martin.malek


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.

like image 31
KryDos Avatar answered Oct 19 '22 00:10

KryDos


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.

like image 1
Thibault Avatar answered Oct 18 '22 22:10

Thibault