

JavaScript-style object literals in PHP - gorm
http://www.phpied.com/javascript-style-object-literals-in-php/

======
Udo
This is made possible by the introduction of closures in PHP 5.3 which I think
was long overdue. However, in this specific case it's worth noting that many
times in JavaScript objects declared like this

    
    
      var fido = {name: "Fido", barks: true};
    

are not actually used as objects but instead emulate a name-value container -
and this is a piece of functionality that PHP already supports natively due to
its array type. Of course it's nice that you can now assemble objects on the
fly, but people coming from JavaScript should be reminded that it's not
necessary to use PHP objects in this fashion most of the time.

 _Edit: it was PHP 5.3, not 5.2_

~~~
phpnode
Nitpick: Closures are a 5.3 feature.

The fact is, this is a fancy, but horrible way to construct your objects if
you care about performance at all. It also breaks things like typehints,
instanceof, inheritance etc.

~~~
Udo
Thanks for the correction. Sometimes new things feel like they've been around
for an eternity ;-)

I absolutely agree with you, that's basically why I wrote the comment. Just
looking at their PHP code, you can sometimes see very clearly in what language
people were thinking at the time. Some people try to emulate Java, JavaScript,
even C or C++ sometimes. It's always horrible.

------
ssttoo
Greetings, author of the post here :)

This post was, of course, just a thought exercise. But if it sounds
intriguing, please check Andrea Giamarchi's much more advanced "JavaScript
Hypertext Preprocessor":
[http://webreflection.blogspot.com/2011/06/javascript-
hyperte...](http://webreflection.blogspot.com/2011/06/javascript-hypertext-
preprocessor.html)

He has stuff like: $Object->prototype->toString->call($o); // [object Object]

~~~
phpnode
Here's what it looks like the other way round, a PHP framework ported to
JavaScript (then swiftly abandoned)

<https://github.com/phpnode/YiiJS/tree/master/js/yii>

~~~
ssttoo
Yeah, there's also phpjs.org when you miss things like strstr() in JS

~~~
phpnode
yeah this port relies heavily on php.js, it's part of the reason it's
abandoned, with all the libs it just becomes too big for client side apps, and
it sits in some kind of uncanny valley that is really nearly the same as the
equivalent php but not quite.

------
jamesmoss
They are introducing shorthand notation for arrays in PHP 5.4 using the []
syntax. For whatever crazy reason using {} notation hasnt been implemented for
objects. I really wonder what the phpinternals team are doing sometimes.

~~~
DanHulton
It took long enough just to get them to adopt [].

------
chip80
Looks very similar to/almost the same of <http://lucato.it/php-anonymous-
objects>

------
fooyc
And now people will think that PHP doesn't have classes.

------
lucian1900
Uh, eww. There's so much accidental complexity in those examples.

One would be better off using nodejs: JavaScript has far fewer terrible flaws
than PHP.

~~~
stan_rogers
And, of course, NodeJS is available at every web hosting company on the planet
for $3.99 a month, so your client will be happy about the ubiquity of the
environment and the cost of getting something off the ground. Isn't it?

You can choose not to _like_ PHP easily. Choosing not to _use_ it is not
nearly as simple. And "ew" is not much of an argument to take to a client who
isn't gambling on being the Next <insert poster child>.

