Live data from Hacker News

Short array syntax finally in PHP 5.4

svn.php.net

61–70 of 80 posts

Re: Short array syntax finally in PHP 5.4

#61
post #18

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 fruitio…

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.

Example:

  $this->link('My Link', array('names'=> array('a', 'b', 'c'), 'values'=> array('a1', 'a2', 'a3') ) );
becomes:

  $this->link('My Link', ['names'=> ['a', 'b', 'c'], 'values'=> ['a1', 'a2', 'a3'] ]);
Just removing the word array makes the code much easier to read, especially when you have deeply nested arrays.

Re: Short array syntax finally in PHP 5.4

#62
post #56

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?

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

#63
post #32
post #24

Earlier quoted context omitted.

They're trying to be nicer to the humans. (Though IMO, any human who wanted to be kind to himself would just not use PHP to begin wtih.)

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.

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.

Re: Short array syntax finally in PHP 5.4

#64
post #36

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 strin…

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.

I use unicode all the time, and I have not had any trouble. All the php functions are binary safe. If you use utf-8 you won't have any touble.

Do you have actual real issues, or do you just like to complain about things you heard other people say?

Re: Short array syntax finally in PHP 5.4

#65
post #36

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 strin…

> cough Unicode anyone?

Why are you coughing about unicode? It works just fine as long as you use utf-8, and there is little reason not to.

Re: Short array syntax finally in PHP 5.4

#67
post #2

That's great news, finally we can do $a = [1, 2, 3]; and it also will make function calls with named parameters much more readable. Sadly, it will be years before the majority of hosting providers adopt 5.4...

> Sadly, it will be years before the majority of hosting providers adopt 5.4...

With VPS hosting as low as $6/month, what reason is there to continue using shared hosting anyways?

Re: Short array syntax finally in PHP 5.4

#68
post #2

That's great news, finally we can do $a = [1, 2, 3]; and it also will make function calls with named parameters much more readable. Sadly, it will be years before the majority of hosting providers adopt 5.4...

> Sadly, it will be years before the majority of hosting providers adopt 5.4... With VPS hosting as low as $6/month, what reason is there to continue using shared hosting anyways?

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

#69
post #56

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?

I think those warnings are very helpful. They help to develop better and more secure code.

Ever tried to assign a non-existing variable in another language? I think you will get a compile error.

This isn't too hard:

  $foo = isset($_POST["foo"]) ? $_POST["foo"] : null;

Re: Short array syntax finally in PHP 5.4

#70
post #65
post #36

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 strin…

> cough Unicode anyone? Why are you coughing about unicode? It works just fine as long as you use utf-8, and there is little reason not to.

What he meant to say was, "I wish PHP has a 'string' type instead of octet buffers".
Post reply on HN