TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

JavaScript-style object literals in PHP

42 点作者 gorm超过 13 年前

6 条评论

Udo超过 13 年前
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<p><pre><code> var fido = {name: "Fido", barks: true}; </code></pre> 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.<p><i>Edit: it was PHP 5.3, not 5.2</i>
评论 #3294540 未加载
ssttoo超过 13 年前
Greetings, author of the post here :)<p>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": <a href="http://webreflection.blogspot.com/2011/06/javascript-hypertext-preprocessor.html" rel="nofollow">http://webreflection.blogspot.com/2011/06/javascript-hyperte...</a><p>He has stuff like: $Object-&#62;prototype-&#62;toString-&#62;call($o); // [object Object]
评论 #3294661 未加载
jamesmoss超过 13 年前
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.
评论 #3295338 未加载
chip80超过 13 年前
Looks very similar to/almost the same of <a href="http://lucato.it/php-anonymous-objects" rel="nofollow">http://lucato.it/php-anonymous-objects</a>
fooyc超过 13 年前
And now people will think that PHP doesn't have classes.
lucian1900超过 13 年前
Uh, eww. There's so much accidental complexity in those examples.<p>One would be better off using nodejs: JavaScript has far fewer terrible flaws than PHP.
评论 #3294519 未加载