Re:Short array syntax finally in PHP 5.4
by gdulli on Sunday July 24, 2011 @11:04AM
(#2799500)
My big complaint about arrays wasn't declaring them. It was the endless array functions you have to use afterwards.
array_push
array_pop
array_slice
array_shift
array_unshift
array_map
array_key_exists
It kind of feels like they missed the point, saving 5 characters when you declare the array once, ignoring all of the other operations you do afterwards.
And this RFC, simple as it is, took 3.5 years to come to fruition.
Re:Short array syntax finally in PHP 5.4
by jonathanyc on Sunday July 24, 2011 @06:14PM
(#2800491)
(Un)fortunately, depending on how you look at it, "everything is an object" is left out in PHP. I personally agree with having some non-object values (use one of the Spl* classes if you want something like that).
Re:Short array syntax finally in PHP 5.4
by code_duck on Sunday July 24, 2011 @04:06PM
(#2800212)
The best step forward for PHP, in my opinion, would be to deprecate all of those fake-namespaced functions, and start treating instances of built-in types as objects. This would also be the chance to fix all the 'needle/haystack, haystack/needle?' type inconsistencies.
The older functions could still exist for backwards compatibility, but people who want to write clean OO code could use the new style. $anArray->map(function(){}), $aString->pos('cow'), etc.
This would be a big step for PHP, and bringing the language closer to parity with Ruby and Python. Not sure how it works with eager type coercion, but JavaScript does it somehow.
Re:Short array syntax finally in PHP 5.4
by wanderr on Sunday July 24, 2011 @04:34PM
(#2800278)
Loose typing plus strong OO for basic types is a pretty horrifying cmbination, IMO. What happens if you call $aString->pos() when PHP decides that $aString can be an int because it happens to be holding a string that's composed of numbers?
Re:Short array syntax finally in PHP 5.4
by code_duck on Sunday July 24, 2011 @04:45PM
(#2800289)
You mean, PHP decides that $aString has to be coerced to an int, not 'can' be, because it's being used in a function that requires an in to make sense? It doesn't matter if it's a string composed of numbers, either - if needed, any string will be coerced regardless of contents, to 0.
What do ECMAScript interpreters do in such a situation?
In the example you gave, this is dependent upon operator precedence. Presumably any method calls are resolved prior to type coercion. This is how PHP and most languages work anyhow, correct? If you do `$this->do_something() + 20`, the method's return value is used. It doesn't try to turn $this into an integer and then call a method upon it. Same for `bcadd($anObj->meth(),34)`. So... I think that isn't a problem.
Re:Short array syntax finally in PHP 5.4
by harisenbon on Monday July 25, 2011 @12:21AM
(#2801330)
While I would love them to bring the array functions more into line with other OO langauges, the entire language is not structured around that concept, so you would be in essence writing a whole new language.
Because almost all the modern PHP frameworks use arrays to pass data, the short-array syntax makes viewing multi-dimensional arrays much easier, and thus will hopefully result in less bugs.
The RFC (https://wiki.php.net/rfc/shortsyntaxforarrays) was actually rejected by the developers, I think it's only really been put in to appease large userland pressure. Find it rather amusing that a 2 change to the parser for an alias would be rejected on 'maintainability' grounds.
Re:Short array syntax finally in PHP 5.4
by birken on Sunday July 24, 2011 @03:22PM
(#2800122)
The voting results on the RFC page is from 2008 when it was rejected. The recent vote that decided its inclusion was here: https://wiki.php.net/todo/php54/vote. I think it had pretty solid support from core developers as well as userland people this time around.
Re:Short array syntax finally in PHP 5.4
by nbpoole on Sunday July 24, 2011 @10:31AM
(#2799438)
With the removal of bad legacy features like register_globals, magic_quotes_gpc, and safe_mode combined with new features like array de-referencing and traits, I'm really excited for PHP 5.4. :)
Re:Short array syntax finally in PHP 5.4
by whiskers on Sunday July 24, 2011 @10:30AM
(#2799436)
It's not quite what you're asking for, but there is a way to handle array results from functions where you know the number of elements that will be returned.
For example:
// explode splits the string on the provided boundary
// and returns an array containing each segment.
// using list() we can assign the two segments to two
Re:Short array syntax finally in PHP 5.4
by wvenable on Sunday July 24, 2011 @08:19PM
(#2800762)
How are any of those examples significantly different from:
list($foo, $bar) = f();
Given that PHP uses the array() keyword for constructing arrays, it is perfectly symmetrical. My IDE highlights the list() keyword just the same as all the other keywords.
Re:Short array syntax finally in PHP 5.4
by paradox95 on Sunday July 24, 2011 @06:58PM
(#2800582)
I don't see how this will ever be useful. The state "echo boo()[1];" assumes you're only ever going to need that one value from the array. The rest of the array is lost. What if the next line in the code needs index 3 from the array? Now you have to run the function again.
I also don't agree that PHP needs constructors to return $this for the same reason. You need to assign the object to a variable so you can reuse it.
$a = new Foo()->bar();
In this case if I later want to call another method on the Foo instance I have recreate it.
Re:Short array syntax finally in PHP 5.4
by etherealG on Sunday July 24, 2011 @07:31PM
(#2800648)
you're assuming that cases don't exist where you won't reuse something. this is a false assumption, e.g. if all I wanted was the creation date of a file: stat("filepath")[7] is a reasonable way to do this in php, but you can't currently.
Re:Short array syntax finally in PHP 5.4
by viraptor on Sunday July 24, 2011 @02:21PM
(#2799961)
Just looked through the RFCs... and I'm a bit surprised noone proposed unification of types yet. It seems a bit silly that the resources, objects, arrays, strings are different types at the language level. They could throw out / reorganise a lot of the randomly named functions this way. Just add the proper methods to the actual types and sort out the (haystack, needle) order, underscores or lack of them, etc.
Re:Short array syntax finally in PHP 5.4
by nknight on Sunday July 24, 2011 @10:15AM
(#2799410)
Uh, why wouldn't
[[4, 5, 6], [7, 8, 9], [10, 11, 12]]
work?
I know PHP has a weird history of rebelling against its C-family-syntax heritage, but requiring the syntax in your example would seem specifically calculated just to make developers' lives harder.
Re:Short array syntax finally in PHP 5.4
by joelhaasnoot on Sunday July 24, 2011 @10:23AM
(#2799427)
Your example would work, but there's a reason you might want different keys. My company's current web app is written in CakePHP, which has a love for arrays. Some of my queries look like this:
Re:Short array syntax finally in PHP 5.4
by Udo on Monday July 25, 2011 @04:25AM
(#2801813)
Personally, I'm quite happy with VPS and would never go back to shared hosting without a good reason. But if you're writing software that is going to be used on lots of "standard hosting" packages you have to take this kind of delay into account.
Re:Short array syntax finally in PHP 5.4
by Tharkun on Sunday July 24, 2011 @03:08PM
(#2800078)
Still strikes me as a useless bit of syntactic sugar. Is it really that much work to write array('foo' => 'bar')? As far as I can tell, writing ['foo' => 'bar'] isn't that much shorter in the first place.
sigh Can the PHP devs focus on the real problems now? cough Unicode anyone? Ridiculously inconsistent naming of string and array functions? The woefully incomplete and useless STL? The lack of OO in arrays and strings?
Re:Short array syntax finally in PHP 5.4
by DanHulton on Sunday July 24, 2011 @08:22PM
(#2800771)
This has been requested by the community for a while now. Synatctic sugar? Definitely. But it's also more.
An actual patch has been submitted for this before, and rejected by the PHP high muck-a-mucks, even though the community desperately wanted it, and the work was already done.
The fact that they finally gave in and included it means not only do we get a nice bit of sugar to sweeten up our programs, but also that maybe the community CAN steer the devs towards issues they care about.
Re:Short array syntax finally in PHP 5.4
by MostAwesomeDude on Sunday July 24, 2011 @03:19PM
(#2800108)
Hey, I've been complaining about PHP's lack of Unicode support forever. Every time I do, though, an apologist comes out of the woodwork to tell me about the mb_*() functions. Apparently the PHP community is quite comfortable not caring about non-ASCII characters.
Andrei Zmievski has been giving this presentation on what happened to Unicode in PHP. A huge amount of work went into making it happen but it eventually could not continue due to several reasons, including design decisions, lack of contributors & interest in the project.
Re:Short array syntax finally in PHP 5.4
by snorkel on Sunday July 24, 2011 @09:51PM
(#2800972)
My complaint about arrays in PHP is warnings for reading a array member that may not exist, such as when reading a form input that might not be there.
$foo = $_POST["foo"];
Undefined array key! Oh, no! Tragedy! A crime has been committed! Are you kidding? Just set $foo to undefined and let me deal with the consequences, please?
Re:Short array syntax finally in PHP 5.4
by nbpoole on Monday July 25, 2011 @12:00AM
(#2801274)
They're notices, not warnings. They can be disabled via a simple call to the error_reporting function.
I fundamentally disagree with you though. For me, notices are very useful for identifying typos and other coding mistakes that can lead to incorrect behavior.
When I run that script with error_reporting set to E_ALL, I get an "undefined index" notice pointing me to the line where I do the comparison: at that point, I can track down where in my code I misspelled the word guest. Without the notice, I might not immediately realize that the 'is_guest' index was mis-spelled somewhere.
Re:Short array syntax finally in PHP 5.4
by code_duck on Monday July 25, 2011 @12:40AM
(#2801375)
You really wouldn't like Python or even stricter languages such as Scala or java, then. In Python, an attempt to access a nonexistent dictionary key will throw an exception which halts execution if not caught. (similar to calling a nonexistent method or function in PHP, but since PHP doesn't have a well-thought out exception system/std. lib as Python does, you can't catch anything in that situation in PHP).
Re:Short array syntax finally in PHP 5.4
by cheald on Monday July 25, 2011 @06:07PM
(#2804933)
In Python, if you're expecting a potentially missing key, you can just use dict.get('key') rather than dict['key']. The equivalent in PHP would be something like:
Being able to say "I expect a potentially nil value (and am okay with that)" via get() rather than array index is both semantically useful, and far more beautiful than the PHP equivalent.
Re:Short array syntax finally in PHP 5.4
by code_duck on Tuesday July 26, 2011 @12:13PM
(#2808006)
Sure, or you can catch a KeyError and do something else entirely. I'm aware of all this, just pointing out some differences to the person to whom I replied.
Python's dict.get is a bit like the getOrElse found in Scala. It's quite handy and can be seen as advanced conceptually.
Re:Short array syntax finally in PHP 5.4
by kylewpppd on Monday July 25, 2011 @12:05AM
(#2801289)
This is exactly what PHP does. Yes, it issues you a notice, not a warning, not an exception. And it does not break execution. Just set your error_reporting variable to a lower level, and you won't get the Notice at all.
Re:Short array syntax finally in PHP 5.4
by ubuntuftw on Monday July 25, 2011 @09:31AM
(#2802517)
I think they're helpful too! Just use isset() to check whether variables exist, and you shouldn't get any errors unless you make a mistake (in which case, wouldn't it be nice to know?).
Re:Short array syntax finally in PHP 5.4
by schiptsov on Sunday July 24, 2011 @11:41PM
(#2801221)
"Perfection is achieved not when there is nothing more to add, but when there is nothing more to cut off."
Adding fancy syntax does not magically transform that crapware to something almost perfect like python3 or ruby (in its way) where these syntactic forms are among foundations of the language and are supported through all basic idioms. ^_^
Re:Short array syntax finally in PHP 5.4
by ma2rten on Sunday July 24, 2011 @01:08PM
(#2799778)
Right now those humans would not choose to start a project in php, but a few years ago there were not too much alternatives. It's not for nothing that the world's second biggest website is written in php.
Re:Short array syntax finally in PHP 5.4
by code_duck on Monday July 25, 2011 @12:47AM
(#2801385)
Do you mean Facebook? They only use PHP for templating and legacy purposes, as far as I understand. Much of their newer code is written in a variety of languages, including C++, Python and Erlang.
array_push
array_pop
array_slice
array_shift
array_unshift
array_map
array_key_exists
It kind of feels like they missed the point, saving 5 characters when you declare the array once, ignoring all of the other operations you do afterwards.
And this RFC, simple as it is, took 3.5 years to come to fruition.
[1,2,3]->push(..) [3,5,6,]->pop(..) [7,8,9]->map(..)
etc..
The older functions could still exist for backwards compatibility, but people who want to write clean OO code could use the new style. $anArray->map(function(){}), $aString->pos('cow'), etc.
This would be a big step for PHP, and bringing the language closer to parity with Ruby and Python. Not sure how it works with eager type coercion, but JavaScript does it somehow.
What do ECMAScript interpreters do in such a situation?
In the example you gave, this is dependent upon operator precedence. Presumably any method calls are resolved prior to type coercion. This is how PHP and most languages work anyhow, correct? If you do `$this->do_something() + 20`, the method's return value is used. It doesn't try to turn $this into an integer and then call a method upon it. Same for `bcadd($anObj->meth(),34)`. So... I think that isn't a problem.
Because almost all the modern PHP frameworks use arrays to pass data, the short-array syntax makes viewing multi-dimensional arrays much easier, and thus will hopefully result in less bugs.
Example:
becomes: Just removing the word array makes the code much easier to read, especially when you have deeply nested arrays.After having to use PHP for a while, it's no coincidence that these days I am using a whole new language.
The RFC (https://wiki.php.net/rfc/shortsyntaxforarrays) was actually rejected by the developers, I think it's only really been put in to appease large userland pressure. Find it rather amusing that a 2 change to the parser for an alias would be rejected on 'maintainability' grounds.
For anyone who's curious, here's a list of the changes in 5.4 alpha 1: http://www.php.net/releases/NEWS_5_4_0_alpha1.txt
http://www.php.net/archive/2011.php#id2011-06-28-1
It's in PHP 5.4 alpha as well ;)
For example:
// explode splits the string on the provided boundary
// and returns an array containing each segment.
// using list() we can assign the two segments to two
// variables within the same statement
$size = "1000x1000";
list( $width, $height ) = explode( "x", $size );
Edit: Yes, it is a special form, but its still irregular, and that makes it shitty.
It's a language construct, not a function.
I also don't agree that PHP needs constructors to return $this for the same reason. You need to assign the object to a variable so you can reuse it.
In this case if I later want to call another method on the Foo instance I have recreate it.My only wish is that they implement something that evaluates {} to (object)array(). It would come in handy when used with anonymous functions.
Anyway, I am pleased with the new syntax, but sadly, I have to use 5.2 as most of the time I'm working on WordPress projects...
Sadly, it will be years before the majority of hosting providers adopt 5.4...
I know PHP has a weird history of rebelling against its C-family-syntax heritage, but requiring the syntax in your example would seem specifically calculated just to make developers' lives harder.
With VPS hosting as low as $6/month, what reason is there to continue using shared hosting anyways?
sigh Can the PHP devs focus on the real problems now? cough Unicode anyone? Ridiculously inconsistent naming of string and array functions? The woefully incomplete and useless STL? The lack of OO in arrays and strings?
An actual patch has been submitted for this before, and rejected by the PHP high muck-a-mucks, even though the community desperately wanted it, and the work was already done.
The fact that they finally gave in and included it means not only do we get a nice bit of sugar to sweeten up our programs, but also that maybe the community CAN steer the devs towards issues they care about.
Time will tell.
Why are you coughing about unicode? It works just fine as long as you use utf-8, and there is little reason not to.
Andrei Zmievski has been giving this presentation on what happened to Unicode in PHP. A huge amount of work went into making it happen but it eventually could not continue due to several reasons, including design decisions, lack of contributors & interest in the project.
Do you have actual real issues, or do you just like to complain about things you heard other people say?
I fundamentally disagree with you though. For me, notices are very useful for identifying typos and other coding mistakes that can lead to incorrect behavior.
Consider the following lines of code:
When I run that script with error_reporting set to E_ALL, I get an "undefined index" notice pointing me to the line where I do the comparison: at that point, I can track down where in my code I misspelled the word guest. Without the notice, I might not immediately realize that the 'is_guest' index was mis-spelled somewhere.Python's dict.get is a bit like the getOrElse found in Scala. It's quite handy and can be seen as advanced conceptually.
I actually added some code like that to a recent post about PHP here: http://news.ycombinator.com/item?id=2771304
Ever tried to assign a non-existing variable in another language? I think you will get a compile error.
This isn't too hard:
What about short object declaration syntax:
$obj = { param:'blah' };
PHP already has stdClass, so I doubt it would take much of a parser change to implement that.
Adding fancy syntax does not magically transform that crapware to something almost perfect like python3 or ruby (in its way) where these syntactic forms are among foundations of the language and are supported through all basic idioms. ^_^
Even better would be bool/float/int/string/func ref type hinting...
Slower parsing, really? Is that what you are worrying about?