MANUAL DE PHP

(y algo mas)windsurf pozo izquierdo
Google
search for in the  
ELMARRAJO.COM mysql bulma desarrollo web linux fedora html ayuda

windsurf mercedes camper

create_function

(PHP 4 >= 4.0.1, PHP 5)

create_function -- Crear una función anónima (estilo-lambda)

Descripción

string create_function ( string args, string codigo )

Crea una función anónima con los parámetros pasados, y devuelve un nombre único para ella. Por lo general los args serán pasados como una cadena delimitada por comillas sencillas, y esto es recomendable también para el codigo. La razón para usar cadenas entre comillas sencillas es evitar que los nombres de variables sean interpretados, de otro modo, si usa comillas dobles será necesario escapar los nombres de variables, p.ej. \$una_var.

Es posible usar esta función para (por ejemplo) crear una función a partir de información recogida en tiempo de ejecución:

Ejemplo 1. Creación de una función anónima con create_function()

<?php
$nueva_func
= create_function('$a,$b', 'return "ln($a) + ln($b) = " . log($a * $b);');
echo
"Nueva funcion anonima: $nueva_func\n";
echo
$nueva_func(2, M_E) . "\n";
// imprime
// Nueva funcion anonima: lambda_1
// ln(2) + ln(2.718281828459) = 1.6931471805599
?>

O, quizás para tener una función de gestión general que pueda aplicar un conjunto de operaciones a una lista de parámetros:

Ejemplo 2. Creación de una función general de procesamiento con create_function()

<?php
function procesar($var1, $var2, $f_matriz)
{
   foreach (
$f_matriz as $f) {
       echo
$f($var1, $var2) . "\n";
   }
}

// crear un grupo de funciones matematicas
$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; }';
$f_matriz = array(
  
create_function('$x,$y', 'return "operacion trigonometrica: ".(sin($x) + $x*cos($y));'),
  
create_function('$x,$y', 'return "una hipotenusa: ".sqrt($x*$x + $y*$y);'),
  
create_function('$a,$b', $f1),
  
create_function('$a,$b', $f2),
  
create_function('$a,$b', $f3)
   );

echo
"\nUsando la primera matriz de funciones anonimas\n";
echo
"parametros: 2.3445, M_PI\n";
procesar(2.3445, M_PI, $f_matriz);

// ahora crear un grupo de funciones de procesamiento de cadenas
$g_matriz = array(
  
create_function('$b,$a', 'if (strncmp($a, $b, 3) == 0) return "** \"$a\" '.
  
'y \"$b\"\n** lucen igual! (mirando los 3 primeros caracteres)";'),
  
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
"\nUsando la segunda matriz de funciones anonimas\n";
procesar("Twas brilling and the slithy toves", "Twas the night", $g_matriz);
?>

y cuando ejecute el código anterior, la salida será:

Usando la primera matriz de funciones anonimas
parametros: 2.3445, M_PI
operacion trigonometrica: -1.6291725057799
una hipotenusa: 3.9199852871011
b*a^2 = 4.8103313314525
min(b^2+a, a^2,b) = 8.6382729035898
ln(a/b) = 0.27122299212594

Usando la segunda matriz de funciones anonimas
** "Twas the night" y "Twas brilling and the slithy toves"
** lucen igual! (mirando los 3 primeros caracteres)
CRCs: -725381282 , 1908338681
similar(a,b) = 11(45.833333333333%)

Pero quizás el uso más común para funciones estilo-lambda (anónimas) es crear llamadas de retorno, por ejemplo cuando se usa array_walk() o usort()

Ejemplo 3. Uso de funciones anónimas como llamadas de retorno

<?php
$av
= array("el ", "un ", "ese ", "este ");
array_walk($av, create_function('&$v,$k', '$v = $v . "mango";'));
print_r($av);
?>

imprime:

Array
(
  [0] => el mango
  [1] => un mango
  [2] => ese mango
  [3] => este mango
)

una matriz de cadenas ordenadas desde la más corta a la más larga

<?php

$sv
= array("corta", "mas larga", "una cadena grande", "es una cosa como una cadena");
print_r($sv);

?>

imprime:

Array
(
    [0] => corta
    [1] => mas larga
    [2] => una cadena grande
    [3] => es una cosa como una cadena
)

ordenarla desde la más larga a la más corta

<?php

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

?>

imprime:

Array
(
    [0] => es una cosa como una cadena
    [1] => una cadena grande
    [2] => mas larga
    [3] => corta
)



add a note add a note User Contributed Notes
create_function
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: ºlambda_2 (the actual first character cannot be displayed)
?>

This means that a anonymous function can't be used recursively. The following code (recursively counting to 10) results in an error:
<?php
   $fn2
= create_function('$a', 'echo $a; if ($a < 10) call_user_func(__FUNCTION__, $a++);');
  
$fn2(1);
  
// Warning: call_user_func(__lambda_func) [function.call-user-func]: First argument is expected to be a valid callback in T:/test/test.php(21) : runtime-created function on line 1
?>
josh at janrain dot com
03-Mar-2006 10:21
Beware! This is merely a convenience function that generates a unique name for a regular function. It is *not* a closure or even an anonymous function. It is just a regular function that gets named for you.
Joshua E Cook
19-Jan-2006 04:43
Functions created by create_function() cannot return a value by reference.  The function below creates a function that can.  The arguments are the same as create_function().  Note that these arguments are passed, unmodified, to eval(), so be sure that data passed in is sanitized.

<?php
/**
 * create_ref_function
 * Create an anonymous (lambda-style) function
 * which returns a reference
 * see http://php.net/create_function
 */
function
create_ref_function( $args, $code )
{
   static
$n = 0;

  
$functionName = sprintf('ref_lambda_%d',++$n);
  
  
$declaration = sprintf('function &%s(%s) {%s}',$functionName,$args,$body);
  
   eval(
$declaration);
  
   return
$functionName;
}
?>
boards at gmail dot com
31-Dec-2005 11:18
If you were checking to see if a function is made properly, this would be a better way of checking:

<?php
$fnc
= @create_function('$arg1,$arg2,$arg3', 'return true;');
# make that function whatever you want
if (empty($fnc)) {
  die(
'Could not create function $fnc.');
}

# although, the follow will NOT work
if (empty(create_function('$arg', 'return $arg;'))) {
  die(
'Could not create anonymous function.');
}
# you would get an error regarding not being able to use a
# return value in writeable context (i.e. a return value is
# a const in C, and the function empty() doesn't use a
# const void* parameter
?>
komori_san at hotmail dot com
04-Nov-2005 07:42
Checking if the function was actually created use something like this:

$newfunc=@create_function($args, $code);
if ($newfunc=='')
{
  // Failed
}
else
{
  // Worked
}
david [at] davelee.com.au
13-Apr-2005 02:15
# dynamically create html helper functions which take the args
# $string_contents, $optional_hash_of_options
# and return the contents wrapped in a tag
  
$html_funcs = Array(
   'table',
   'tr',
   'th',
   'td',
   'div',
   'span',
   'pre',
   'strong',
   'em'
);
$args = '$html, $options=Array()';
$code = '
  $o = "";
  foreach ($options as $a => $b) {
   $o .= " $a=\"$b\"";
  }
  return "<$tag$o>$html</$tag>";
';
foreach ($html_funcs as $key => $tag) {
   ${$tag} = create_function($args, "\$tag = '$tag'; $code");
}

# usage example:

print $table(
  $tr($th('heading').$td('this is the cell content')),
  Array('style'=>'border: 1px solid silver;')
);
endofyourself at yahoo dot com
07-Oct-2004 12:17
You really should avoid using this as well as you should avoid using eval(). Not only will there be a performance decrease but can it lead to obfuscation and bad coding habits. There is almost always an alternative solution to self modifying code.
MagicalTux at FF.ST
09-Mar-2004 10:25
neo at gothic-chat d0t de wrote :
Beware of memory-leaks, the garbage-collection seems to 'oversee' dynamically created functions!

Not really...

In fact, PHP can not "unassign" functions. So if you create a function, it won't be deleted until the end of the script, even if you unset the variable containing its name.

If you need to change a part of a function everytime you run a loop, think of a way to make a more general function or try using eval :) (functions are made to be re-used. If you need to run your own piece of code once, eval is much better).
neo at gothic-chat d0t de
20-Jan-2004 08:54
Beware of memory-leaks, the garbage-collection seems to 'oversee' dynamically created functions!

I used a function like this to replace special characters in links with their htmlentities:
<?php
$text
= preg_replace_callback (
  
"/(<(frame src|a href|form action)=\")([^\"]+)(\"[^>]*>)/i",
  
create_function (
      
'$matches',
      
'return $matches[1] . htmlentities ($matches[3]) . $matches[4];'
  
),
  
$text);
?>

After 1000 calls, the process used about 5MB more than before. In my situation this boosted up the memory-size of one PHP-process up to over 100MB!

In such cases, better store the function in a global variable.
nospam at fiderallalla dot de
08-Aug-2003 06:39
Sometimes it may be useful to create functions in a dynamic environment
(f. e. in a daemon-like php script).

Normally declaring a function must be done once, which results in the problem,
that in this special case modifying a function wouldn't have an effect until the script is reloaded.

Maybe this code snipplet is useful 4 u.

File: "functions.inc"
<?php
function test($str) {
   echo
$str;
}
?>

Dynamic FunctionHandler:
<?
$FileName
= "functions.inc";
$FileHandle = fopen($FileName,"r");
$FileContent = fread($FileHandle,filesize($FileName));
fclose($FileHandle);

preg_match_all("#function\ ?([a-zA-Z0-9-_]*)\ ?\((.*?)\)\ ?\{(.*?)\}#mixse",$FileContent,$Matches);
if (
is_array($Matches) && isset($Matches[0]) && count($Matches[0]) > ) {
     foreach (
$Matches[0] as $key=>$val ) {
       $
$Matches[1][$key] = create_function($Matches[2][$key],$Matches[3][$key]);
   }
}
?>

The Test:
<?php echo $test("test"); ?>
.. will echo "test";

Hans Kuhlen
DB on music_ml at yahoo dot com dot ar
02-Apr-2003 04:58
What I posted above is logical because anonymous functions don't inherit the method scope. You'll have to do this:

<?php
class AnyClass {
 
 var
$classVar = 'some regular expression pattern';

 function
classMethod() {

  
$_anonymFunc = create_function( '$arg1, $arg2', 'if ( eregi($arg2, $arg1) ) { return true; } else { return false; } ' );

  
$willWork = $_anonymFunc('some string', $classVar);
 
 }

}
?>
DB on music_ml at yahoo dot com dot ar
02-Apr-2003 02:04
Apparently you can't refer to a class variable from an anonymous-defined function, inside a class method, using the $this keyword:

<?php
class AnyClass {
 
  var
$classVar = 'some regular expression pattern';

  function
classMethod() {

  
$_anonymFunc = create_function( '$arg', 'if ( eregi($this->classVar, $arg) ) { return true; } else { return false; } ' );

  
$wontWork = $_anonymFunc('some string');
 
  }

}
?>

This would throw a warning on 'undefined variable: this'...
listes at brissambre dot org
14-Aug-2002 10:22
[Editor's note: Only regular variables are serialized (scalars, arrays, objects), and as lambda functions are not stored as any of those types, it is not saved during session serialization.]

Warning, it seems that you can't store such lambda functions in Sessions, because only the function's name will be stored, not the function itself.
So don't save the function but only it's code and call create_function each time the script is called.
ben-php at bacarisse dot btinternet dot co dot uk
26-Jul-2001 01:33
A nice technique for building complex search patterns on lists, files or whatever is to build function combining functions like this:

<?php
function _not_($f) {
  return
create_function('$x',
  
"return !$f(\$x);");
}

function
_and_($f, $g) {
  return
create_function('$x',
  
"return $f(\$x) && $g(\$x);");
}
?>

(similarly for _or_ and others...).  Once you've built your matching primitives you can then build more complex matches into your script.

Unfortunately, as explained in (closed) bug #10721, the function names returned by create_function have a null byte at the front and this causes a parse error.

You can fix the definition like this:

<?php
function _not_($f) {
 
$f = substr($f, 1);
  return
create_function('$x',
  
"return !call_user_func(chr(0).'$f', \$x)");
}
?>

The expression that re-builds the function name avoid the null being literally in the parsed string.  If there is a better fix, please let me know.
x-empt[a_t]ispep.cx
05-Jul-2001 11:41
Create_function enables the ability to change the scope of functions.  You might have a class where it needs to define a GLOBAL function.  This is possible, like:

<?php
      
class blah {
               function
blah() {
                      
$z=create_function('$arg1string','return "function-z-".$arg1string;');
                      
$GLOBALS['z']=$z;
               }
       }
      
$blah_object=new blah;

      
$result=$GLOBALS['z']('Argument 1 String');
       echo
$result;
?>

Making a function escape it's defined scope can be useful in many situations.
maxim at maxim dot cx
27-Apr-2001 03:26
for those who want to assign it's own name to a function consider this code:

<?php
$fname
= 'hello';

$func = sprintf('
   function %s($v="") {
       Return "$v<BR>";
   }
'
,
$fname
);

eval(
$func);
echo
$fname('Please print it.... please....');
?>

what it does is,
: Creats a function as a string;
: Replaces the function name with $fname value;
: Converts the string into a REAL php code with eval()
: Calls the function using the variable function as declared before ($fname);

Simple, isn't it?

Can work well as an abstraction layer for portability and/or compatibility purposes

Maxim Maletsky
maxim@maxim.cx // PHPBeginner.com
mrben at free dot fr
20-Feb-2001 08:33
Here is another tricky but usefull techynote, good for adding "plugin" to a existing class :

<?

class Hoge {
   var
$lamda;
   var
$text;

   function
set($lamda)
   {
      
$this->lamda = $lamda;
   }

   function
callLamda()
   {
      
$func = $this->lamda;
       return
$func($this);
   }

   function
get()
   {
       return
$this->text;
   }
}

$newfunc = create_function('&$class', 'echo $class->get();' );

$h = new Hoge;
$h->text = "Hi there !";
$h->set($newfunc);
$h->callLamda();

?>
koyama at hoge dot org
14-Dec-2000 04:22
How do you use function which is created by create_function() as class method?

<?php
class Hoge {
  var
$lamda;
  function
set($lamda) {
  
$this->lamda = $lamda;
  }
  function
callLamda() {
  
$func = $this->lamda;
   return
$func();
  }
}

$newfunc = create_function('', 'echo "hoge<br>\n";');

$h = new Hoge;
$h->set( $newfunc );
$h->callLamda();
?>

It works fine. :-)

Citas célebres

La belleza es la llave de los corazones; la coquetería es la ganzúa.

P. Masson
Citas en tu mail
©Contenidos Gratis

Ilusiones Opticas
ilusion_optica_051.jpg
Contenidos Web

Chiste de... Parejas
Groserías

- No voy a volver a salir con Guillermo.

- ¿Por qué, hija?

- Porque sabe canciones de letras muy groseras.

- ¿Y se atreve a cantártelas?

- No, pero las silba.
Chistes en tu mail
©ContenidosGratis

Humor Gráfico
humor_grafico_040.jpg
Contenidos Web

Inicio | Acción | Estrategia | Palabras | Puzzles | Solitarios | Foro Trucos
Cake ManiaCake Mania
Jugadores: 6835
Categoría del juego: Acción
Objetivo del juego: Ayuda a Jill a recuperar la pastelería de su abuela llevando su propia pastelería; consigue clientes y gana dinero.
Rainbow WebRainbow Web
Jugadores: 2199
Categoría del juego: Puzzles
Objetivo del juego: Rompe un pegajoso hechizo y salva un reino de fantasía en Rainbow Web. Tendrás toneladas de diversión mientras juegas a este mágico desafío para la mente.
Mahjongg FortunaMahjongg Fortuna
Jugadores: 12462
Categoría del juego: Solitarios
Objetivo del juego: Velocidad y habilidad mental son las armas más importantes en esta versión de un antiguo juego asiático. Despeja el tablero lo antes posible haciendo clic en las fichas iguales y gánate la fama eterna de la puntuación más alta.
Chainz 2Chainz 2
Jugadores: 6955
Categoría del juego: Puzzles
Objetivo del juego: Entra en el mundo de las combinaciones con Chainz 2: Relinked, emocionante secuela del exitazo del año pasado, Chainz. Gira eslabones y crea combinaciones de 3 ó más.
DeliciousDelicious
Jugadores: 4405
Categoría del juego: Acción
Objetivo del juego: ¿Eres un as de la multitarea? ¿Quieres que tus clientes estén contentos? ¡Pues Delicious es tu juego! Sacia el apetito de los clientes y tenlos contentos; ¡no te arriesgues!
BookwormBookworm
Jugadores: 4568
Categoría del juego: Palabras
Objetivo del juego: Junta las letras para formar palabras. ¡Las palabras más largas valen más puntos!
ZumaZuma
Jugadores: 4976
Categoría del juego: Acción
Objetivo del juego: Controla el ídolo de la rana de piedra de los antiguos Zuma en este intrigante enigma de acción. ¡Dispara bolas para formar conjuntos de tres, pero si dejas que lleguen a la calavera dorada morirás!
Jewel of AtlantisJewel of Atlantis
Jugadores: 3798
Categoría del juego: Puzzles
Objetivo del juego: Descubre la ciudad hundida de la Atlántida y busca valiosos tesoros. Viaja más allá de las profundidades del mar y vive trepidantes aventuras en Jewel of Atlantis.
Jewel QuestJewel Quest
Jugadores: 3727
Categoría del juego: Puzzles
Objetivo del juego: Convierte la arena de la antigua selva en oro tan rápido como puedas juntando grupos de 3 elementos. ¡Los grupos más grandes valen más puntos!
Bejeweled 2Bejeweled 2
Jugadores: 3659
Categoría del juego: Puzzles
Objetivo del juego: Con cuatro modos de juego únicos y fascinantes, nuevas piezas de juego explosivas e imponentes fondos planetarios, Bejeweled 2 es mucho más adictivo que nunca.
Contenidos gratis en tu webSiguiente >>

Fotos divertidas
fotos_increibles_0169.jpg
Contenidos Web
microrobots avion deportes riesgo recetas cocina canaria juegos online gratis moto motociclismo horoscopos naranjas valencianas surf canarias montañismo ciudades turismo postales gratis library Horoscopos Diarios Windsurf Canarias
fregadero microondas placa electrica bañopreparar camper pantalla plananevera compresor electricacamper fiat ducato camper baño quimicomampara enrollable bañocamper aire climatizadofurgoneta surf windsurffurgoneta surf windsurftelevisor furgonetas camperfurgonetas camper cama

Sudoku del día
Nivel de dificultad: Fácil



Cómo jugar:
El juego consiste en colocar los números del 1 al nueve de tal forma que no se repita el mismo número en la columna, fila y caja (bloques 3x3 enmarcados).

©Contenidos Gratis | Sudoku en tu mail
Sucedió el...

30 de agosto de 1617

Fallece Santa Rosa de Lima, por la que se celebra el "Día de la Patrona de América".
Efemérides en tu mail
©Contenidos Gratis
windsurf canarias youtube porno canarias baleares valencia madrid fallera mayor campus party alcacer feria valencia fernando alonso loterias dinero inversiones violencia de genero makro empresas cartera soledad tolerancia metro valencia gobierno de españa violencia de genero UIMP navidad