Just a few changes in 5.4 & 5.5 you might find interesting, some pretty cool new features.
Short array syntax
Function array dereferencing
Method call by array
Binary notation
The next ones are all available in 5.5 (unstable/trunk).
Try/catch/finally
Foreach into a list
Array/string dereferencing
GENERATORS!
Motherfuckin' generators bro! About time they got this implemented.
Assuming DB is some generic class providing interface to the DB.
$pointer is some pointer/cursor in the DB.
hasNext specifies if there are further rows to fetch.
fetch fetches the next row.
Basically the yield line will return that value ($pointer->fetch()), then next time the function is called (or in this case, iterated through), it will resume execution at that same line. Until that while loop ends.
J
Short array syntax
PHP:
$a = [1,2,3,4];
echo $a[0];
$a = ['a' => 1, 'b' => 2];
echo $a['a'];
Function array dereferencing
PHP:
function test()
{
return [1,2,3];
}
echo test()[0]; // Won't work <5.4
Method call by array
PHP:
$method = ['SomeClass', 'someMethod'];
$method(); // SomeClass::someMethod()
Binary notation
PHP:
$n = 0b1101; // 13
The next ones are all available in 5.5 (unstable/trunk).
Try/catch/finally
PHP:
try
{
someFunc(); // If this throws an exception or not, the finally block still runs
}
catch(Exception $e)
{
echo 'exception occurred';
}
finally
{
cleanup();
}
Foreach into a list
PHP:
$array = [
[1, 10],
[2, 20],
];
foreach($array as list($a, $b))
{
// $a is 1, $b is 10 on first iteration
}
Array/string dereferencing
PHP:
echo "abc"[0]; // will echo 'a'
echo [1,2,3][1]; // will echo 2
GENERATORS!
Motherfuckin' generators bro! About time they got this implemented.
PHP:
function search($keywords)
{
$pointer = DB::query("SELECT * FROM tbl WHERE name = %s", $keywords);
while($pointer->hasNext())
{
yield $pointer->fetch(); // This is the part which makes it a generator
}
}
foreach(search('xyz') as $result)
{
echo $result['name'];
}
Assuming DB is some generic class providing interface to the DB.
$pointer is some pointer/cursor in the DB.
hasNext specifies if there are further rows to fetch.
fetch fetches the next row.
Basically the yield line will return that value ($pointer->fetch()), then next time the function is called (or in this case, iterated through), it will resume execution at that same line. Until that while loop ends.
J