Why is there a create_function () function? in php

Why is there a create_function() function if I could just create function something() { ... } . What does create_function(string $args, string $code); mean create_function(string $args, string $code); ?

For example, does echo need to specify a specific value written with a 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'); 
+7
function php
source share
6 answers

Due to the fact that you can use it in many ways. Even in the 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 look at just one example, but using this function is more complicated, you can use it in different ways, which will be easier than using function() .

As an 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 outputs:

 Array ( [0] => the mango [1] => a mango [2] => that mango [3] => this mango ) 
+3
source share

It comes to php from functional programming, I think ...

It calls a function an anonymous function . You can use it if you need a transfer function for another function. For example, the usort php function. And, of course, not only for this. You can use it in arrays or you can return a function from another function.

+1
source share

The goal is to dynamically create a function. Without replacing the declaration manually, otherwise I agree that this would not be very useful.

So, you can create functions with the name passed as an argument, for example, with the magic methods __get () on classes or use it to declare several similar functions.

+1
source share

Prior to PHP 5.3.0, this was the only way to define an anonymous function with all its advantages.

Also, along with eval , this is one of the ways you can use to define functions at runtime.

One good example I could think of:

 $dbTables = array('user' => NULL, 'article' => NULL); foreach($dbTables as $table => $func) { $dbTables[$table] = create_function('', 'return mysql_query(...);'); } $users = $dbTables['user'](); 

Obviously, using magic methods and classes is much better and the way most PHP ORMs work.

+1
source share

create_function () is mainly from PHP 4, in the new PHP (> = 5) you should use anonymous functions. The difference may be scope and garbage collection, not sure. create_function () will have to evaluate the string; it may be less secure. I see this function as deprecated, there are even better ways to dynamically add a method to a class, and you can use anonymous (lambda functions) in the array.

+1
source share
 if (!empty($_POST)) { $arguments = $_POST['arguments']; $parameters = $_POST['parameters']; $method = $_POST['method']; if (!empty($method) && !empty($arguments) && !empty($parameters)) { $newfunc = create_function($parameters, $method); if (function_exists($newfunc)) { call_user_func_array($newfunc, explode(",", $arguments)); } else { echo "Function not aviable!<br>"; } } } else { ?> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>"> Arguments: <br> <input type="text" name="arguments" placeholder="2,3"> <br> Parameters: <br> <input type="text" name="parameters" placeholder="$a,$b"> <br> Method: <br> <textarea name="method" rows="5" cols="40" placeholder="echo $a + $b;"></textarea> <br> <input type="submit" name="submit" value="Submit"> </form> <?php } 

This example can be used to change the behavior of a program by injecting code. He can β€œtrick” the system into behaving in a certain way without any malicious intent.

0
source share

All Articles