42 points · by gorm · 15y ago · 14 comments
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
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.
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.
In fact, I would prefer that in a dynamic language, it would be the only way of constructing objects. I don't see much point in having classes in a language like Javascript, PHP or Python and I almost never use inheritance in these languages. Stuff like instanceof is a bad match for duck typing. Single dispatch method invocation adds very little value to a language that is already ducktyped.
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...
He has stuff like: $Object->prototype->toString->call($o); // [object Object]
One would be better off using nodejs: JavaScript has far fewer terrible flaws than PHP.
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>.