Status
Not open for further replies.

JmZ

(╯°□°)╯︵ ┻━┻
1,789
2008
729
0
Just a few changes in 5.4 & 5.5 you might find interesting, some pretty cool new features.

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
 
9 comments
I like short array syntax. Generator / yield keyword would make things lot faster + efficient. PHP 5.4 upload progress support is simply awesome.
 
Introduction of generators is pretty much to remove the need for writing bulky iterator implementations.

Currently (5.4 and below), you have to implement one of the iterator interfaces and all of the required methods. This makes things much simpler.

There's also a new password hashing API in 5.4 or 5.5 I believe, but I haven't mentioned it here. People use sha1/md5 too much (including even vBulletin and such). They're not made for hashing passwords, they are too 'fast', a good password hashing algorithm is slow (e.g. blowfish).
 
PHP 5.4 also includes a development server (like it's provided in Django (Python) and Rails (Ruby))

PHP:
cd /path/to/project
php -S localhost:8000
 
Im already running 5.4 (not that Ive made much use of traits/5.4 features due to my policy of developing for the largest common denominator), im hoping fields makes it into 5.5. Its in RFC still at the moment.

5.4 has huge performance (memory related) improvements over 5.3, for that reason alone people should switch.
 
Those kinds of changes in my opinion are nice but for the people, who maintain the code and upgrade it, will have a much harder time finding things. Like dropping the word array. Brackets are used everywhere and now searching for array definitions could take tenfold the effort. So coding is quicker and maybe cheaper but maintenance will be double or triple or ??
 
Status
Not open for further replies.
Back
Top