Upgrading from create_function to closures - php

Struggling to understand closures for a couple of days. Can anybody point me in the right direction? Need to re-write this "create_function" as a lambda.
$section = preg_replace_callback('/{listing_field_([^{}]*?)_caption}/', create_function('$matches', 'global $config,$or_replace_listing_id,$lang;require_once($config[\'basepath\'].\'/include/listing.inc.php\'); return listing_pages::renderSingleListingItem($or_replace_listing_id, $matches[1],\'caption\');'), $section);

You define a closure like so:
$myClosure = function ($args) {
// do something;
};
create_function takes two arguments - the first is an eval'd string of the callable's arguments, the second is the code to execute - so you'd do something like this:
$section = preg_replace_callback(
// Your regex search pattern
'/{listing_field_([^{}]*?)_caption}/',
// Your callback
function ($matches) use ($config, $or_replace_listing_id) {
require_once $config['basepath'] . '/include/listing.inc.php';
return listing_pages::renderSingleListingItem(
$or_replace_listing_id,
$matches[1],
'caption'
);
},
// Your subject
$section
);
Note that I've replaced your global variable calls with importing them via use into the callback instead, and removed $lang because you aren't using it.

Related

Callback in function (PHP) is not working

When I execute following code I am getting this error. Why is that? What is the proper use of callbacks?
CODE (simplified)
class NODE {
//...some other stuff
function create($tags, $callback=false) {
$temp = new NODE();
//...code and stuff
if($callback) $callback($temp); //fixed (from !$callback)
return $this;
}
}
$document = new NODE();
$document->create("<p>", function($parent) {
$parent->create("<i>");
});
ERROR
Fatal error: Function name must be a string in P:\htdocs\projects\nif\nif.php on line 36
$document->new NODE();
This is not valid syntax. The accepted format would be:
$document = new NODE();
In addition to this, if you use the unary operator (!) on a false, you get true. If you use it on a Callable, you get false. As such, if (!$callback) $callback() will throw the first error of your script.
As a side note, you are reinventing the wheel. I would strongly recommend you take a look at the DOMDocument family of classes, which are doing exactly what you are currently trying to implement, albeit with fewer callbacks.
if(!$callback) $callback($temp);
If $callback is false, for sure you won't be able to call it as a callback.
if(!$callback) $callback($temp);
should probably be
if($callback) $callback($temp);
And the instanciation:
$document = new NODE();
My 2c here, type hinting may be good to use here as well.
Ex: function create($tags, callable $callback = function())
To do such a thing in php you should use function pointers and tell php which function to execute.
Look at this code.
// This function uses a callback function.
function doIt($callback)
{
$data = acquireData();
$callback($data);
}
// This is a sample callback function for doIt().
function myCallback($data)
{
echo 'Data is: ', $data, "\n";
}
// Call doIt() and pass our sample callback function's name.
doIt('myCallback');
So as you seen you can only pass the name to the function and you should predefine the function..
Similar question: How do I implement a callback in PHP?

Php closure function as parameter to preg_replace_callback results in Internal Server Error 500

I have a preg_replace_callback which takes a closure (anonymous) function as 2nd parameter and it works perfectly fine on local, but when I deploy it to live environment it results in error => Internal server error 500. When i remove the Closure it works.
$regExPattern = '/\<%(?<content>.*?)%\>/';
$template = preg_replace_callback($regExPattern, function ($matches) use ($dataItem) {
if(isset($dataItem[trim($matches['content'])])) {
return $dataItem[trim($matches['content'])];
}
else {
return '';
}
}, $template);
Any suggestions how can i work arround this problem. I need to use $dataItem inside my callback function and pass it to preg_replace_callback.
My development environment is code igniter.
Anonymous functions only work in PHP 5.3 and up. You could use create_function() instead:
$regExPattern = '/\<%(?<content>.*?)%\>/';
$template = preg_replace_callback($regExPattern, create_function(
'$matches'
, 'if(isset($dataItem[trim($matches[\'content\'])])) {
return $dataItem[trim($matches[\'content\'])];
}
else {
return "";
}'
)
);
Untested, of course.

Problem with using preg_replace_callback

i have a problem when i use preg_replace_callback. i have google translator class
and i want to translate all matches using it .
the code was .
$code = preg_replace_callback('/_e\(\'(.*?)\'\)/',create_function(
'$matches',
'return $translator->translate($matches);'),
$code);
when i make var dump for the var $code, i found its string"1" !!!
im sure that im using a right way for the class.
Thanks.
The problem here is scope. Something similar to this would work in JavaScript, but JS and PHP handle scope differently. To access $translator from within the anonymous function's scope, you need to declare it as a global.
<?php
$code = preg_replace_callback('/_e\(\'(.*?)\'\)/',
create_function('$matches',
'global $translator;'.
'return $translator->translate($matches);'),
$code);
?>
If you want to keep the anon as a one-liner, you can use the globals array:
<?php
$code = preg_replace_callback('/_e\(\'(.*?)\'\)/',
create_function('$matches',
"return $GLOBALS['translator']->translate($matches);"),
$code);
?>
If you have PHP 5.3.0 or later, this can be alleviated with closures and use:
<?php
$code = preg_replace_callback('/_e\(\'(.*?)\'\)/',
function($matches) use ($translator) {
return $translator->translate($matches);
}, $code);
?>
This is assuming that $translator was created in the same scope as $code.
In PHP 5.3 you could use a Closure.
<?php
$code = preg_replace_callback(
'/_e\(\'(.*?)\'\)/',
function($matches) use ($translator) {
return $translator->translate($matches);
},
$code
);
Try to also pass the $translator as argument.
This could look like:
$code = preg_replace_callback('/_e\(\'(.*?)\'\)/',create_function(
'$translator,$matches',
'return $translator->translate($matches);'),
$code);
UPDATE: This code example does not work. The replace callback is invoked with only one argument while the anonymous function here expects 2 arguments. The working implementation would be:
$code = preg_replace_callback('/_e\(\'(.*?)\'\)/',create_function(
'$matches',
'global $translator; return $translator->translate($matches);'),
$code);

Can temporary functions/macros be created in PHP?

Take a look at the following illustration:
// Trims input, fixes spaces and encodes bad glyphs. Also works with arrays.
function prepare_param($param)
{
$retval = "";
function prc($param)
{
$r = split(" ", trim($param));
foreach($r as $i => $e)
$r[$i] = urlencode($e);
return join("+", $r);
}
// If input is an array
if(is_array($param))
{
$retval = array();
foreach($param as $e)
$retval[] = prc($e);
}
// If input is a string
else if(is_string($param))
{
return prc($param);
}
else throw new Exception("Invalid input! Expected String or Array.");
}
Obviously the function prc will now be declared globally, even though declared inside a function. Is there a way to follow this principle, creating a tiny function/macro inside another function as not to litter the global scope? The alternative would be to make a class with a private function, which seems like overkill for my use.
Any help appreciated
You probably want closures, which are anonymous functions.
If you have PHP 5.3, enter anonymous functions:
$prc = function($param)
{
$r = split(" ", trim($param));
foreach($r as $i => $e)
$r[$i] = urlencode($e);
return join("+", $r);
};
if(is_array($param))
{
$retval = array();
foreach($param as $e)
$retval[] = $prc($e);
}
else if(is_string($param))
{
return $prc($param);
}
In this case, $prc only lives in the scope of your prepare_param() function.
If you have access to >=PHP 5.3, you can use anonymous functions, and if not, you can use create_function.
If you don't have PHP 5.3, you can use the create_function function.
There are two ways to do so. The closures/anonymous functions are possible from PHP 5.3, and the oldschool way would be to use create_function() - which is quite fugly.
However in your case, you don't want either. There is no benefit in creating or recreating the function. You just need it once, as it does not depend on any initialization state. The idiom you should use is called "dererred definition" and possible in PHP with:
if (!function_exists("prc")) {
function prc($param) {
...
}
}
You should name it with its parent function as prefix however (e.g. prepare__prc) to avoid clashes and to signalize its internal use.
Oh, and btw it could also be simplified compacted into:
$param = join("+", array_map("urlencode", split(" ", trim($param))));
anonymous functions might be what you are looking for
<?php
$greet = function($name)
{
printf("Hello %s\r\n", $name);
};
$greet('World');
$greet('PHP');
?>
If you don't use php 5.3 please be aware of the fact that the memory allocated by the "create_function()" function isn't released until the php process finishes. So if you create a lot of functions you might be running into issues.

How do I implement a callback in PHP?

How are callbacks written in PHP?
The manual uses the terms "callback" and "callable" interchangeably, however, "callback" traditionally refers to a string or array value that acts like a function pointer, referencing a function or class method for future invocation. This has allowed some elements of functional programming since PHP 4. The flavors are:
$cb1 = 'someGlobalFunction';
$cb2 = ['ClassName', 'someStaticMethod'];
$cb3 = [$object, 'somePublicMethod'];
// this syntax is callable since PHP 5.2.3 but a string containing it
// cannot be called directly
$cb2 = 'ClassName::someStaticMethod';
$cb2(); // fatal error
// legacy syntax for PHP 4
$cb3 = array(&$object, 'somePublicMethod');
This is a safe way to use callable values in general:
if (is_callable($cb2)) {
// Autoloading will be invoked to load the class "ClassName" if it's not
// yet defined, and PHP will check that the class has a method
// "someStaticMethod". Note that is_callable() will NOT verify that the
// method can safely be executed in static context.
$returnValue = call_user_func($cb2, $arg1, $arg2);
}
Modern PHP versions allow the first three formats above to be invoked directly as $cb(). call_user_func and call_user_func_array support all the above.
See: http://php.net/manual/en/language.types.callable.php
Notes/Caveats:
If the function/class is namespaced, the string must contain the fully-qualified name. E.g. ['Vendor\Package\Foo', 'method']
call_user_func does not support passing non-objects by reference, so you can either use call_user_func_array or, in later PHP versions, save the callback to a var and use the direct syntax: $cb();
Objects with an __invoke() method (including anonymous functions) fall under the category "callable" and can be used the same way, but I personally don't associate these with the legacy "callback" term.
The legacy create_function() creates a global function and returns its name. It's a wrapper for eval() and anonymous functions should be used instead.
With PHP 5.3, you can now do this:
function doIt($callback) { $callback(); }
doIt(function() {
// this will be done
});
Finally a nice way to do it. A great addition to PHP, because callbacks are awesome.
Implementation of a callback is done like so
// This function uses a callback function.
function doIt($callback)
{
$data = "this is my data";
$callback($data);
}
// This is a sample callback function for doIt().
function myCallback($data)
{
print 'Data is: ' . $data . "\n";
}
// Call doIt() and pass our sample callback function's name.
doIt('myCallback');
Displays: Data is: this is my data
One nifty trick that I've recently found is to use PHP's create_function() to create an anonymous/lambda function for one-shot use. It's useful for PHP functions like array_map(), preg_replace_callback(), or usort() that use callbacks for custom processing. It looks pretty much like it does an eval() under the covers, but it's still a nice functional-style way to use PHP.
well... with 5.3 on the horizon, all will be better, because with 5.3, we'll get closures and with them anonymous functions
http://wiki.php.net/rfc/closures
You will want to verify whatever your calling is valid. For example, in the case of a specific function, you will want to check and see if the function exists:
function doIt($callback) {
if(function_exists($callback)) {
$callback();
} else {
// some error handling
}
}
create_function did not work for me inside a class. I had to use call_user_func.
<?php
class Dispatcher {
//Added explicit callback declaration.
var $callback;
public function Dispatcher( $callback ){
$this->callback = $callback;
}
public function asynchronous_method(){
//do asynch stuff, like fwrite...then, fire callback.
if ( isset( $this->callback ) ) {
if (function_exists( $this->callback )) call_user_func( $this->callback, "File done!" );
}
}
}
Then, to use:
<?php
include_once('Dispatcher.php');
$d = new Dispatcher( 'do_callback' );
$d->asynchronous_method();
function do_callback( $data ){
print 'Data is: ' . $data . "\n";
}
?>
[Edit]
Added a missing parenthesis.
Also, added the callback declaration, I prefer it that way.
For those who don't care about breaking compatibility with PHP < 5.4, I'd suggest using type hinting to make a cleaner implementation.
function call_with_hello_and_append_world( callable $callback )
{
// No need to check $closure because of the type hint
return $callback( "hello" )."world";
}
function append_space( $string )
{
return $string." ";
}
$output1 = call_with_hello_and_append_world( function( $string ) { return $string." "; } );
var_dump( $output1 ); // string(11) "hello world"
$output2 = call_with_hello_and_append_world( "append_space" );
var_dump( $output2 ); // string(11) "hello world"
$old_lambda = create_function( '$string', 'return $string." ";' );
$output3 = call_with_hello_and_append_world( $old_lambda );
var_dump( $output3 ); // string(11) "hello world"
I cringe every time I use create_function() in php.
Parameters are a coma separated string, the whole function body in a string... Argh... I think they could not have made it uglier even if they tried.
Unfortunately, it is the only choice when creating a named function is not worth the trouble.

Categories