PHP 5.4.0 released
php.net
php.net
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
They've also removed magic_quotes. A few functions that queried its status still exist, but you can't turn it on.
It did not "get out of hand" by any measure. Once there was a consensus that this feature is needed by the significant part of PHP community, it was implemented. Could it be done earlier? Maybe, so could a lot of other nice things.
funcReturnsFunc(1)(2)
PHP's parser is... not exactly a great work of software engineering.
FTFY. :)
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!
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.
I spend a lot of time programming in PHP. Its reputation is pretty bad, but updates like this keep me going!
Would love to get back into it now that it's a "real" language.
Got any good tips about books on modern PHP programming?
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!
If you're going to use PHP5, Symfony2 is a well-reasoned, best-of-breed system.
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.
http://www.amazon.com/Objects-Patterns-Practice-Experts-Sour...
- 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!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.?<?=$username;?>
(Personal Preference Alert!) Is easier to read, faster to type and easier for designers to handle than:
<?php echo $username;?>
The downside is, if you deploy your code on a server you don't have access to (meaning you can't enable short tags via php.ini) then you'd have to painfully replace every "<?=" with "<?php echo" BY HAND, find and replace is not allowed.
Eventually, there was a big argument over whether short open tag should be removed in PHP6 (it's not), the biggest reasons for removing it (from what I remember) was that it could conflict with the "<?xml" tag and if you don't have access to php.ini or a modern code editor, you're screwed.
The removal of one of these major problems (by forcing short open tag to work regardless of php.ini settings) brings PHP's short open tag one step closer to being loved and admired by everyone, including YOU, so get used to thinking things like this are great.
As far as chained string offsets, who cares! Long live short open tag! (Sorry I couldn't help it.)
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")
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.
This allows short tags to be disabled for XML clash-avoidance while still allowing the user to use the short-form of echo.
How is it weird and stupid? <?php echo $var ?> is better than <?= $var ?> ?
It makes sense when you think of it in offsets. $string[offset] means that given $a = 'foo' then $a[0] is f and given $a = 'f' then $a[0] is f. Thus $a[0][0] would be the first offset of the first offset of "foo". $a[0][1] would not be valid, of course.
Also it's not really something you should ever use, but it's definitely more consistent than throwing an error.
I understand for general php portability that its better to use the <?php echo vs <?=$whatever syntax, but less code is a good thing.
This is good news indeed, they seem to be actually improving the language for once.
"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()."
function foo($opts) {
echo $opts['source'] . $opts['destination'];
}
// just one extra pair of [] needed instead of new array()
foo(['source' => '/123', 'destination' => '/abc']);Unless you've ever used languages with actual first-class keyword arguments (Python) and languages using hashes as pseudo-kwargs (Ruby, JavaScript). Then you know it's no substitute.
Also, you could already do just that in PHP before, it simply took 5 more characters.
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.
Absolutely. But PHP devs have repeatedly refused to support named parameters so this is a good enough fix.
The story of PHP's life.
foo(array('source' => '/123', 'destination' => '/abc'));
Short syntax will make it less repetitive though. You can also use something like array_merge to emulate jQuery's $.extend for creating defaults.(edit: talking about JS only.)
> 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? 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[0] http://docs.php.net/manual/en/closure.bindto.php
[1] https://developer.mozilla.org/en/JavaScript/Reference/Global...
and the UPGRADING instructions: http://svn.php.net/viewvc/php/php-src/tags/php_5_4_0/UPGRADI...
http://gcov.php.net/viewer.php?version=PHP_5_4
Why even bother having a test suite?
* 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.
finally ....
Nice
Good ol' Debian, on the other hand, might or might not get it in time for the Wheezy freeze. CentOS? Forgetaboutit.