

PHP 5.4.0 released - 0x0
http://php.net/downloads.php#v5

======
randomdrake
Big hitters:

array dereferencing - $object->method()[$index] is now valid syntax

built-in webserver via CLI - _php -s_ \-
<http://php.net/manual/en/features.commandline.webserver.php>

traits and the insteadof operator - <http://php.net/language.oop5.traits.php>

shortened array syntax - $array = [1, 2, [1, 2, 3], 4];

Finally _removed_ register_globals ;)

Default charset of UTF-8!

Read all about it! <http://www.php.net/manual/en/migration54.php>

~~~
shaunxcode
I am curious what the internal hang up is which prevents:

funcReturnsFunc(1)(2)

~~~
masklinn
> I am curious what the internal hang up is which prevents:

PHP's parser is... not exactly a great work of software engineering.

~~~
bazerka
> PHP is... not exactly a great work of software engineering.

FTFY. :)

~~~
smsm42
You can do better? As smart man as you are surely can figure out where to send
the patches.

------
orthecreedence
This is a nice list of improvements. As someone who spends a lot of the day in
PHP-land, this is going to make me happy. As someone who also controls our
entire server stack, waiting for someone else to decide to upgrade will not be
a problem :)

I'm glad the PHP devs are starting to actually make it a "real" language. It
seems like PHP has always been the fat kid bumbling along behind everyone
shouting "wait up guys!!" With closures in 5.3, syntax improvements and
removal of _horrible_ features (register_globals, magic_quotes, etc) in 5.4,
it's shaping up quite nicely.

Thanks, PHP devs!

~~~
Pengwin
> waiting for someone else to decide to upgrade will not be a problem :)

It is sad that a lot of servers are not like this, and PHP 5.4 will take ages
to appear, if at all.

Heck, I know of a hosting company still using PHP4.

~~~
agildehaus
Hosting company that still uses PHP4? Name names so we can avoid and chastise
them!

~~~
alexseman
Media Temple for one. For a domain or subdomain you had to explicitly choose
PHP5 instead of the default PHP4.

~~~
joshuacc
They actually changed this recently. The default is now PHP5, with an option
to select PHP4.

------
dirkdeman
They also improved the zend engine, CURL extension, added Tokyo cabinet
abstract and Berkeley DB Support! Added a couple of hashes, Improved JSON
Extension (JSON_NUMERIC_CHECK!) and a whole lot of bug fixes. The UTF-8 part
was long overdue, glad they fixed that.

I spend a lot of time programming in PHP. Its reputation is pretty bad, but
updates like this keep me going!

~~~
itmag
PHP was my first love (actually my second, after QBasic) when I was a
teenager.

Would love to get back into it now that it's a "real" language.

Got any good tips about books on modern PHP programming?

~~~
dirkdeman
Books... not so much. I have another idea: you could check out an MVC-
framework like codeigniter or CakePHP. There are numerous tutorials about
them, and they give you the added benefit of working with an MVC pattern
(knowledge you can use in other languages and frameworks as well, such as Ruby
on Rails) and working Object Oriented.

In my opinion, a big drawback of PHP is that it's so easy to screw up because
it's easy to pick up. There are so many tutorials out there which will let you
make a procedural, cross-scripted, sql-injected security liability spaghetti
code... Starting with CodeIgniter or CakePHP is not very hard, especially when
you have a little understanding on basic programming with PHP. Good luck!

~~~
eropple
CodeIgniter and CakePHP are great examples of code badly brought forward into
PHP5 from PHP4 roots. I wouldn't recommend either to either a novice or an
expert; CodeIgniter is a fantastic example of out-of-date practices and
CakePHP an example of writing such abjectly slow code that everybody else
benchmarks against you to look good.

If you're going to use PHP5, Symfony2 is a well-reasoned, best-of-breed
system.

~~~
latimer
I'd also recommend Kohana. It was originally a fork of CodeIgniter, but
version 3.0 was a complete rewrite for PHP5. Compared to Symfony, it's more of
a barebones framework.

~~~
eropple
I wouldn't. Kohana's track record of cavalier response to security concerns is
off-putting and their code is not particularly impressive.

There's a reason people who use PHP are getting on @fabpot's train and not
Kohana's, and that's because Symfony2 does much, much more _right_ than anyone
else in the conversation.

------
tlack
Other great little changes:

    
    
      - you can now do function()[0] 
      - <?= always on despite what you say in the INI
      - safe_mode always off
    

Seems like a return to sanity for the PHP team, at least temporarily.. I even
kinda like the traits!

~~~
klausa
How is '<?=' being always on great change? I always thought it was weird and
kind of stupid, but I'm genuinely curious how is that great thing.

Also,

    
    
      Chained string offsets - e.g. $a[0][0] where $a is a string - now work.
    

I have no idea how is that supposed to work. What should be the outcome on,
say:

    
    
      $a = "foo"; $a[0][0]
    

What about (assuming $a is still "foo"):

    
    
      $a[0][1]
    

etc.?

~~~
nolok
> How is '<?=' being always on great change? I always thought it was weird and
> kind of stupid, but I'm genuinely curious how is that great thing.

When you share your code with various clients (open source, software seller,
...), you can not assume that short array are on, thus you have to use <?php
echo $var ?> everytime instead of the shorter <?=$var?>. Having the second
syntax always working solves that, this is on the same level as the short
array syntax change, removing a hurdle and letting you concentrate on more
important stuff.

> $a = "foo"; $a[0][0]

$a is a string "foo" $a[0] is a string "f" equating to substr($a, 0, 1)
$a[0][0] is a string "f" equating to substr($a[0], 0, 1)

> $a[0][1]

This will give an undefined index error, same thing as $a[42] for exemple,
since index 1 doesn't exists in $a[0] (= "f")

~~~
pavel_lishin
> thus you have to use <?php echo $var ?> everytime instead of the shorter
> <?=$var?>

Is this really a huge concern? I guess I just have $tockholm syndrome at this
point, and am used to writing <?php echo $var; ?> explicitly every time,
precisely for the reasons you mentioned. It's one less thing to worry about.

------
TazeTSchnitzel
They finally added short array syntax, removed register_globals and magic
quotes, and the default character set is now UTF-8!

This is good news indeed, they seem to be actually improving the language for
once.

------
CptCodeMonkey
Seems like the multiyear unit-test coverage campaign is starting to pay off
for the core PHP engineers, 5-6 years ago they swore they couldn't do
shortened array syntax without major problems.

------
sbarre
Also awesome:

"Arrays cast from SimpleXMLElement now always contain all nodes instead of
just the first matching node. All SimpleXMLElement children are now always
printed when using var_dump(), var_export() and print_r()."

------
hinathan
I'm glad to see the short array syntax. Having strayed from PHP for a while to
work on Ruby and JavaScript I'm really enjoying the ability to leave out
semicolons in unambiguous end-of-line situations. Would love to see that make
it into PHP at some point.

~~~
chime
The best part of short array syntax is that now you don't need named
parameters to be supported by the language.

    
    
        function foo($opts) {
          echo $opts['source'] . $opts['destination'];
        }
        
        // just one extra pair of [] needed instead of new array()
        foo(['source' => '/123', 'destination' => '/abc']);

~~~
adorton
While I agree that it makes it easier to hack in that functionality, it would
still be very useful to have named parameters for functions that don't
implement that functionality.

For example, fgetcsv takes parameters for delimiter, enclosure, and escape
char (as of 5.3). Right now, if I want to specify a different enclosure, I'm
forced to specify a delimiter even if I still want to use the default. I'd
much prefer the ability to tell the function call that I want to override the
enclosure and leave the delimiter alone.

~~~
chime
> While I agree that it makes it easier to hack in that functionality, it
> would still be very useful to have named parameters for functions that don't
> implement that functionality.

Absolutely. But PHP devs have repeatedly refused to support named parameters
so this is a good enough fix.

~~~
pavel_lishin
> this is a good enough fix.

The story of PHP's life.

------
function_seven
From here: <http://www.php.net/manual/en/migration54.other.php>

> Chained string offsets - e.g. $a[0][0] where $a is a string - now work.

Can someone explain why this is? I'm sure I'm misunderstanding, but this seems
to mean that the following would work:

    
    
      $a = "Hello";
      echo $a[0]; //"h"
      echo $a[0][0]; // also "h"??
    

What's the point? To avoid failure if I pass a string instead of a
2-dimensional array?

~~~
fooyc
Consistency. Since $a[0] is a string, you would expect $a[0][0] to work too.

~~~
function_seven
Thanks. Let me rework my original question then. Why the hell didn't it always
work?

    
    
      php > $a = "Hello";
      php > echo $a[0];
      H
      php > echo $a[0][0];
      PHP Fatal error:  Cannot use string offset as an array in php shell code on line 1

~~~
tlack
PHP always seemed to have something weird in the parser about subscripts/array
indexes. It's been there since I started using it during PHP3. Another similar
fix they performed is making func()[0] work - before you had to do $tmp =
func(); $tmp[0]. An old, old bug finally fixed.

------
fooyc
Closures can now have a `$this` (by default it is the class in which the
closure is declared). No more $that = $this hacks :)

~~~
dave1010uk
There's also Closure::bindTo() [0], which should allow you do do slightly more
functional programming, like JavaScript's Function.apply() [1]

[0] <http://docs.php.net/manual/en/closure.bindto.php>

[1]
[https://developer.mozilla.org/en/JavaScript/Reference/Global...](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/apply)

------
evilvoidhamster
These features can only be good for PHP. Now the community needs to step up
and care for the language better. We have seen the popularity of many
languages and frameworks rise and fall, but php keeps chugging along. Modern
frameworks are awesome, but the community is still rather fractured. I would
love to see people more proud of php, to get over the fads around them. Now
the language is getting some nice features, I think it's our time to shine :)

------
0x0
Here are the NEWS text: [http://svn.php.net/viewvc/php/php-
src/tags/php_5_4_0/NEWS?re...](http://svn.php.net/viewvc/php/php-
src/tags/php_5_4_0/NEWS?revision=323659&view=markup)

and the UPGRADING instructions: [http://svn.php.net/viewvc/php/php-
src/tags/php_5_4_0/UPGRADI...](http://svn.php.net/viewvc/php/php-
src/tags/php_5_4_0/UPGRADING?revision=323659&view=markup)

------
mahmud
Can it add hex now?

<https://bugs.php.net/bug.php?id=61095>

~~~
greut
the ticket says Closed, so it better do.

------
petdance
They shipped it with 82 failing tests.

<http://gcov.php.net/viewer.php?version=PHP_5_4>

Why even bother having a test suite?

~~~
fruchtose
I'm not sure what's more worrying:

* 82 failing tests

* 44 _expected_ test failures

* 1119 compiler warnings

I want to believe that PHP is growing up, but I have to admit I'm not
convinced.

------
llgrrl_
One thing that's pretty important too is PHP-FPM. I've been dreaming about
this for a long time for my nginx deployments.

<http://php-fpm.org/>

~~~
chrisguitarguy
Hasn't PHP-FPM been part of the PHP core for a while now? Since 5.3.3 (mid
2010) according the PHP FPM site.

~~~
Mikushi
Yes but has of 5.4 it is now considered stable, whereas before it was still
experimental (even tho it worked fine).

------
CWIZO
When will this be available for FreeBSD? I really can't wait for traits, short
array syntax and "foo()[0]". This is really a nice upgrade. Good job PHP team!

------
rdoherty
Are there any benchmarks (synthetic or real-world) for the performance
improvements? Would have been nice if the release notes had provided some.

~~~
robryan
There are some around like <http://news.php.net/php.internals/57760>, seen a
couple more but forget where. The trend does seem to be towards far better
performance across the board though which is good.

------
pavelkaroukin
I see a lot of mentions of zend in extension names, function names.. Could
someone elaborate how Zend relates to PHP?

~~~
dirkdeman
Zend developed the Zend Engine, a virtual machine. PHP4 is based on the Zend
engine. Zend is more than just the framework, it literally is the core of PHP.

------
jaequery
array dereferencing - $object->method()[$index] is now valid syntax

finally ....

~~~
jaequery
shortened array syntax - $array = [1, 2, [1, 2, 3], 4]; ..... can't express
how long i waited for these

------
emehrkay
Added callable typehint.

Nice

~~~
roel_v
Oh that's a nice one I hadn't noticed yet. Now all that's left is type hints
for scalars...

------
kijin
Now the question is, when will this become available in mainstream Linux
distributions? I guess Arch et al. will get it pretty quickly, followed by
Ubuntu 12.10 or maybe 13.04 depending on how many packages it affects.

Good ol' Debian, on the other hand, might or might not get it in time for the
Wheezy freeze. CentOS? Forgetaboutit.

~~~
sanswork
You mention two distros that are designed for stability. Thats very different
from following the bleeding edge and they have their purpose and reason for
it(Which while developers like myself and I assume you sometimes forget, our
server admin friends never do). If you want to follow the leading edge there
is always also the option of making your own packages.

