Serialization is the conversion of object states, arrays and simple variables into a String type to allow transmission and storage for later use or by another script. PHP provides the serialize() and unserialize() functions to deal with this but after experimenting with anonymous functions (also called closures) which I blogged about earlier, I found out that trying to serialize a closure threw an exception because anonymous functions are actually internal PHP Closure objects that have serialization disabled.
I thought - "there's a gap that needs to be filled", and set out to write my second class for phpclasses.org but after a quick google search I found this (Extending PHP 5.3 Closures with Serialization and Reflection), and I must say, I'm glad I'm not the one who wrote the code for this class. The code is complex and brilliant, check it out!
Showing posts with label Anonymous Functions. Show all posts
Showing posts with label Anonymous Functions. Show all posts
Thursday, 4 March 2010
Monday, 22 February 2010
PHP now supports anonymous functions
Did you know PHP 5.3 now supports anonymous functions? Let's look at some code.
This is the old way how you used to do it:
<?php
//the array we're working with
$arr = array(1,2,3,4,5);
//declare a function for use as callback
function callback($i){
echo $i;
}
//use the callback
array_walk($arr, 'callback'); //outputs 12345
?>
Now, this is the new way:
<?php
$arr = array(1,2,3,4,5);
//use anonymous callback method
array_walk($arr, function($i){echo $i;}); //outputs 12345
?>
And here's another interesting thing you can now do with PHP:
<?php
$arr = array(5,4,3,2,1);
//create a variable which will be a reference to a new function later
$fn = null;
//$fn's function is declared within the arguments of another function
array_walk($arr, $fn = function($i){echo $i;}); //outputs 54321
//calls the new function in $fn
$fn(68); //outputs 68
?>
Did you know you could use external variables inside of closures? You need the use keyword and pass the variables into it separated by commas like so:
$basePath = "/usr/home/";
$getPathFor = function($name) use ($basePath) {return "$basePath$name";}
echo $getPathFor("bob"); // /usr/home/bob
Nice.
This is the old way how you used to do it:
<?php
//the array we're working with
$arr = array(1,2,3,4,5);
//declare a function for use as callback
function callback($i){
echo $i;
}
//use the callback
array_walk($arr, 'callback'); //outputs 12345
?>
Now, this is the new way:
<?php
$arr = array(1,2,3,4,5);
//use anonymous callback method
array_walk($arr, function($i){echo $i;}); //outputs 12345
?>
And here's another interesting thing you can now do with PHP:
<?php
$arr = array(5,4,3,2,1);
//create a variable which will be a reference to a new function later
$fn = null;
//$fn's function is declared within the arguments of another function
array_walk($arr, $fn = function($i){echo $i;}); //outputs 54321
//calls the new function in $fn
$fn(68); //outputs 68
?>
Did you know you could use external variables inside of closures? You need the use keyword and pass the variables into it separated by commas like so:
$basePath = "/usr/home/";
$getPathFor = function($name) use ($basePath) {return "$basePath$name";}
echo $getPathFor("bob"); // /usr/home/bob
Nice.
Subscribe to:
Posts (Atom)