I am a fan of php, ever since my first software development job where I was thrown in the deep end and had to sink or swim. I swam in the sea of php and actually think it is a very nice language (begin receiving hate mail.... now!). You see, I never knew the non-OOP php, I was brought into a well managed project with Classes and Interfaces, it was basically Java with dollar-sigils thrown in. I never suffered through the spaghetti nightmare that so much php is, so I'm admittedly wearing some rose-colored glasses.
There is one feature that I love about php, it is the height of elegance, the associative array. The associative array can do anything, need a stack just use array_push and array_pop, need to do some functional programming array_map, array_filter, and array_walk are your friends. The beauty of the associative array in php is that it is ubiquitous, its a hash map, array, stack, object, all rolled into one.
The problem with the associative array though is that its create syntax is ugly. Here is how you create an array in php
[php]
$example = array(1, 2, 3);
[/php]
That doesn't seem so bad, what am I complaining about, well the problem comes when you have nested arrays (which is somewhat common).
[php]
$configuration =
array(
'name' => 'My Configuration',
'server' => array (
'name' => 'localhost',
'port' => '8080',
'users' => array (
array (
'username' => 'example',
'password' => 'example'
),
array (
'username' => 'user01',
'password' => 'pass01'
)
),
'overrides' => array (
'https' => false )
)
);
[/php]
The biggest problem is not that I have to keep writing the word array its that arrays don't look like arrays. They look like function calls, with other nested function calls, of course the newlines and tabs help out, but they don't solve the problem. Let's look at how this would appear in JavaScript using super-awesome JSON.
[javascript]
var configuration = {
name: 'My Configuration',
server: {
name: 'localhost',
port: '8080',
users: [ {username: 'example', password: 'example'},
{username: 'user01', password: 'pass01'} ],
overrides: {https: false}
}
};
[/javascript]
That's much nicer and the more important part is that hashes look like hashes and arrays look like arrays. There was a proposed php syntax change that was discussed to death and an analysis here which would have made the following legal php
[php]
$configuration = [
'name' => 'My Configuration',
'server' => [
'name' => 'localhost',
'port' => '8080',
'users' => [ ['username' => 'example', 'password' => 'example'],
['username' => 'user01', 'password' => 'pass01'] ],
'overrides' => ['https' => false]
]
];
[/php]
But this is not the case, and more importantly, may never be the case. The issue has been brought up and rejected several times, with the following reasoning
I see no advantages here, only another way to do already possible thing and yet another way to confuse people.
- Anthony Dovgal
For the record: I'm -1. array() is enough. Ridiculous idea to begin with.
- Jani Taskinen
There is no reason to add another syntax for *exactly* the same thing, especially because it's ungoogable
- Derick Rethans
Then there is this
I'm ok with it as well. Like I said over a year ago (*), it is a syntax very familiar to web developers and it feels natural to most people.
(*) http://marc.info/?l=php-internals&m=117060700805108&w=2
- Rasmus Lerdorf (the guy who created PHP)
The problem I see here is that the vote finally breaks down to 1 / 3 contributors in favor and 17 / 20 users in favor, it was then rejected. There is no recourse and as long as the people maintaining php see this as a useless feature, as a "backward incompatible syntax that duplicates already existing one, but 5 characters shorter" change request, it will never move forward.
This is a shame and it is myopic and dishonest. To trivialize this change supported by the vast majority of the php users voting as some sort of luxury people want so that they can avoid typing 5 characters is intellectually dishonest. The major point is that square brackets mean arrays in php, unless you are creating one, then it looks like a function.
[php]
$array = array(0, 1, 2, 3, 4, 5, 6); //Create the array
$element = $array[5]; //Get the element at index 5
$array[3] = "hello"; //Set the element at index 3
$array[] = "example"; //Push an element onto the array
[/php]
The dishonest part about the argument against the change is that it would be some sort of crazy zany backwards incompatible change and we would never break backwards compatibility. First off, if you are going to classify any addition to the language as backwards incompatible, then stop having a core development team, you are done, close up shop you can't add anything to the language anymore. Secondly, php has a history of removing and adding things that break older code, anyone remember the first stab at classes, or ever written $stringvar{4} to access a character, that is going away and you should write $stringvar[4] from now on. It's deprecated as of php 5.3.0 and will most likely be removed as of php 6.
But we are left as a community without recourse, the array function to create an array exists and by that fact alone it will remain forever. The most disheartening thing about the whole misadventure is that because this has been brought up and rejected so many times any time it is brought up now it is automatically rejected as being rejected before. Discussion has attached to this idea a Scarlett Letter, most undeservedly.
At the end of the day I won't jump ship, but it's one of those things that makes you stop and scratch your head. Why be so against it? I don't buy the arguments against it, I don't understand the fervor that the devs fight against it with, and I don't see the harm.
As people push the boundaries of what the language can do and the concept of the DSL (pushed by those ruby people) becomes more and more prominent, language cruft will have to fall by the wayside. Not improving array literal syntax is not the end of the world, but the attitudes and egos on display fighting a harmless change that the user base wants could be.
Edit: My arrays were using non-literal keys, which is incorrect as pointed out by Mewp in the comments, they have been corrected. Sorry about that, thanks for the correction Mewp.
Your code is broken. You shouldn't write an array like:
ReplyDelete'value');
?>
The reason is, php doesn't know wheter it's a constant, or a string. Consider following code:
'value');
?>
See http://pl.php.net/manual/en/language.types.array.php#language.types.array.donts
Apart from that, as a user of php, I agree --- dealing with arrays is one of the more annoying things in php.
also, your input sanitization seems to have thrashed my code examples.
ReplyDeleteRepeating them:
first was:
array(key => 'value');
second:
define('key', 'qwerty');
array(key => 'value');
use htmlspecialchars instead of strip_tags (or some strange regex deleting everything between LT and GT)
@Mewp input is being sanitized by WordPress which I don't muck around in the guts of seeing as they have more experience protecting against various attack vectors than I do. You are right about the array keys, that will teach me to go from json to php in the same post.
ReplyDelete