Web студия "GrandView"
  Главная   Написать Контакты
   
   
О проекте
Руководство php
 

create_function

(PHP 4 >= 4.0.1, PHP 5)

create_function -- Создаёт анонимную (lambda-style) функцию

Описание

string create_function ( string args, string code )

Создаёт анонимную функцию из переданных параметров и возвращает её уникальное имя. Обычно args передаются строкой в апострофах, что также рекомендуется для параметра code. Основанием для использования строк в апострофах является защита имён переменных от обработки, другими словами, если вы будете использовать кавычки, вам будет нужно экранировать все имена переменных вот так: \$avar.

Вы можете использовать эту функцию (к примеру) для создания функций на основании информации, полученной во время выполнения программы:

Пример 1. Создание анонимной функции с помощью create_function()

<?php
$newfunc
= create_function('$a,$b', 'return "ln($a) + ln($b) = " . log($a * $b);');
echo
"New anonymous function: $newfunc\n";
echo
$newfunc(2, M_E) . "\n";
// outputs
// New anonymous function: lambda_1
// ln(2) + ln(2.718281828459) = 1.6931471805599
?>

Или, возможно, чтобы иметь общий обработчик для применения набора функций к списку параметров:

Пример 2. Создание функции-обработчика с помощью create_function()

<?php
function process($var1, $var2, $farr)
{
    for (
$f=0; $f < count($farr); $f++) {
        echo
$farr[$f]($var1, $var2) . "\n";
    }
}

// create a bunch of math functions
$f1 = 'if ($a >=0) {return "b*a^2 = ".$b*sqrt($a);} else {return false;}';
$f2 = "return \"min(b^2+a, a^2,b) = \".min(\$a*\$a+\$b,\$b*\$b+\$a);";
$f3 = 'if ($a > 0 && $b != 0) {return "ln(a)/b = ".log($a)/$b; } else { return false; }';
$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)
    );

echo
"\nUsing the first array of anonymous functions\n";
echo
"parameters: 2.3445, M_PI\n";
process(2.3445, M_PI, $farr);

// now make a bunch of string processing functions
$garr = array(
   
create_function('$b,$a', 'if (strncmp($a, $b, 3) == 0) return "** \"$a\" '.
   
'and \"$b\"\n** Look the same to me! (looking at the first 3 chars)";'),
   
create_function('$a,$b', '; return "CRCs: " . crc32($a) . " , ".crc32(b);'),
   
create_function('$a,$b', '; return "similar(a,b) = " . similar_text($a, $b, &$p) . "($p%)";')
    );
echo
"\nUsing the second array of anonymous functions\n";
process("Twas brilling and the slithy toves", "Twas the night", $garr);
?>

Вышеприведённый код выводит следующее:

Using the first array of anonymous functions
parameters: 2.3445, M_PI
some trig: -1.6291725057799
a hypotenuse: 3.9199852871011
b*a^2 = 4.8103313314525
min(b^2+a, a^2,b) = 8.6382729035898
ln(a/b) = 0.27122299212594

Using the second array of anonymous functions
** "Twas the night" and "Twas brilling and the slithy toves"
** Look the same to me! (looking at the first 3 chars)
CRCs: -725381282 , 1908338681
similar(a,b) = 11(45.833333333333%)

Однако, вероятно, наиболее обоснованное использование lambda-style (анонимных) функций - это создание функций-обработчиков, например, при использовании array_walk() или usort()

Пример 3. Использование анонимной функции как функции-обработчика

<?php
$av
= array("the ", "a ", "that ", "this ");
array_walk($av, create_function('&$v,$k', '$v = $v . "mango";'));
print_r($av);
?>

выводит:

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

Сортировка строк по их длине

<?php

$sv
= array("small", "larger", "a big string", "it is a string thing");
print_r($sv);

?>

результат:

Array
(
  [0] => small
  [1] => larger
  [2] => a big string
  [3] => it is a string thing
)

Теперь отсортируем их в обратном порядке:

<?php

usort
($sv, create_function('$a,$b','return strlen($b) - strlen($a);'));
print_r($sv);

?>

результат:

Array
(
  [0] => it is a string thing
  [1] => a big string
  [2] => larger
  [3] => small
)



func_get_arg> <call_user_func
Last updated: Sat, 27 Jan 2007
 
add a note add a note User Contributed Notes
create_function
colin dot mckinnon at gmail dot com
20-Aug-2007 05:51
In response to koyama at hoge dot org (14-Dec-2000):

This does NOT create a new method - try adding this at the end:

if (function_exists($h->lamda)) {
   print "Its a function\n";
} else {
   print "No it isnt";
}

It creates a function which $h->lamda points to.

Under PHP4 you could simply add an argument $this which meant it *behaved* like a method (though it existed in global scope) but with PHP5, you can't have a variable named $this in a function (which is rather irksome).

(and methinks the ant-bot challenge is taking the mickey - min(three, four)?  !).
TSE-WebDesign
18-Aug-2007 09:55
Here's how to call a runtime-created function from another runtime-created function:
<?php
        $get_func
= create_function('$func', 'return substr($func,1);');
       
$get_value = create_function('$index','return pow($index,$index);');
       
$another_func = create_function('$a', '$func="\x00"."'.$get_func($get_value).'";return $func($a);');
        echo
$another_func(2); # result is 4
?>
kkaiser at revolution-records dot net
13-Apr-2007 01:10
In the process of migrating a PHP4 codebase to PHP5, I ran into a peculiar problem. In the library, every class was derived from a generic class called 'class_container'. 'class_container' contained an array called runtime_functions and a method called class_function that was as follows:

function class_function($name,$params,$code) {

  $this->runtime_functions[$name] = create_function($params,$code);

}

In a subclass of class_container, there was a function that utilized class_function() to store some custom lambda functions that were self-referential:

function myfunc($name,$code) {

  $this->class_function($name,'$theobj','$this=&$theobj;'.$code);

}

In PHP4, this worked just fine. The idea was to write blocks of code at the subclass level, such as "echo $this->id;", then simply $MYOBJ->myfunc("go","echo $this->id;"); and later call it like $MYOBJ->runtime_functions["go"]();

It essentially worked exactly like binding anonymous functions to objects in Javascript.

Note how the "$this" keyword had to be manually redefined for the $code block to work.

In PHP5, however, you can't redeclare $this without getting a fatal error, so the code had to be updated to:

function myfunc($name,$code) {

  $this->class_function($name,'$this',$code);

}

Apparently create_function() allows you to set $this via a function argument, allowing you to bind anonymous functions to instantiated objects. Thought it might be useful to somebody.
a dot steenveld at id dot umcn dot nl
18-Jan-2007 06:18
It is possible to use this call to implement continuations but you need a small workaround for a nagging feature of create_function(). The result of this function does start with a null character which might result in loosing the name of your function altogether!. (See also bug report #40160)

Here is a bit of code to play with. The result should be 'f(2, 2) = 6'
<?php
/* continuations in php.
   vim:nu

   Code inspired by http://www.ps.uni-sb.de/~duchier/python/continuations.html
*/

function writeln($s) { echo "$s\n"; }

function
lambda0 ($args, $code)
    { return
substr(create_function ($args, $code), 1);
    }
function
L ($l)
    { if (
strncmp($l, 'lambda_', 7) === 0) return "\0$l";
      else return
$l;
    }
   
function
mul ($x, $y, $c) { $f = L($c); $f($x*$y); }
function
add ($x, $y, $c) { $f = L($c); $f($x+$y); }
function
mal ($x, $y, $c) { mul(2, $x, lambda0 ('$v', "add(\$v, $y, $c);")); }

function
f($x, $y)
    {
mal($x, $y, lambda0 ('$v', "writeln(\"f($x, $y) = \$v\");"));
    }   

   
f(2, 2);
?>
Dan D
23-Oct-2006 07:22
Beware when using anonymous functions in PHP as you would in languages like Python, Ruby, Lisp or Javascript.  As was stated previously, the allocated memory is never released; they are not objects in PHP -- they are just dynamically named global functions -- so they don't have scope and are not subject to garbage collection.

So, if you're developing anything remotely reusable (OO or otherwise), I would avoid them like the plague.  They're slow, inefficient and there's no telling if your implementation will end up in a large loop.  Mine ended up in an iteration over ~1 million records and quickly exhasted my 500MB-per-process limit.
Phlyst
06-Oct-2006 05:10
In reply to info at adaniels dot nl:

You may not be able to use __FUNCTION__ in a lambda (thanks for pointing it out; I was having that problem just now), but you can use $GLOBALS to work around it if you're assigning the function to a variable. I reimplemented array_walk_recursive() in PHP4 like this:

<?php
$array_walk_recursive
= create_function('&$array, $callback',
   
'foreach($array as $element) {
        if(is_array($element)) {
            $funky = $GLOBALS["array_walk_recursive"];
            $funky($element, $callback);
        }
        else {
            $callback($element);
        }
    }'
);
?>
Josh J
27-Sep-2006 12:45
In regards to the recursion issue by info at adaniels dot nl

Anon function recursion by referencing the function variable in the correct scope.
<?php
$fn2
= create_function('$a', 'echo $a; if ($a < 10) call_user_func($GLOBALS["fn2"], ++$a);');
$fn2(1);
?>
info at adaniels dot nl
10-May-2006 03:42
Note that using __FUNCTION__ in a an anonymous function, will always result '__lambda_func'.

<?php
    $fn
= create_function('', 'echo __FUNCTION__;');
   
$fn();
   
// Result: __lambda_func
   
echo $fn;
   
// Result:
Новости
11 июля 2007
Сайт запущен
© 2007 info@grandviewstudio.com

Deprecated: Function set_magic_quotes_runtime() is deprecated in /home/sites/grandviewstudiocom/www/65f67d67a94ad980786580ae69e11c07/sape.php on line 324

Deprecated: Function set_magic_quotes_runtime() is deprecated in /home/sites/grandviewstudiocom/www/65f67d67a94ad980786580ae69e11c07/sape.php on line 330
Z058440144362 Z348613067571