TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

XHP: A New Way to Write PHP (from Facebook)

134 pointsby ashuover 15 years ago

18 comments

ivankiriginover 15 years ago
XHP rocks so fucking hard, it isn't even funny. It is just so much better than alternatives.<p>IMHO, It is the only PHP tool I use at facebook that is better than alternatives in other languages. I'm looking at you, django templates!<p>The notation perfectly represents the objects, with no cruft associated with object oriented programming. That is really rare. You could argue that the markup syntax is cruft, but it really helps code readability to have two types of syntax, for code and for markup.<p><pre><code> $b = &#60;span&#62;quotes and variables&#60;/span&#62;; $a = &#60;div&#62;omg, I can't "believe" how easy these {$b} are&#60;/div&#62;;</code></pre>
评论 #1113936 未加载
评论 #1114709 未加载
评论 #1114504 未加载
评论 #1115016 未加载
评论 #1114433 未加载
jbyersover 15 years ago
For me, XHP is far more interesting than HipHop. And I say that as someone who administers a pile of single-application CPU-bound PHP servers. This completely and forever changes the templates-vs-just-PHP debate, and I'm glad -- it's the kind of evolution PHP needs to continue to be taken seriously.
评论 #1114306 未加载
评论 #1114434 未加载
jerfover 15 years ago
In a previous job, we used Apache::ASP, which is basically PHP-style &#60;? ?&#62; tags, only with Perl in the insides. We got an awful lot of mileage out of simply re-writing the default &#60;?= ?&#62; equivalent to automatically HTML-escape the contents, and adding a new &#60;?! ?&#62; type thing to pass through the inside unescaped.<p>It's less "cool" than this, certainly, but making the default reasonably safe and forcing you to ask for the dangerous level of output rather than defaulting to dangerous and having to ask for safe is a lot easier to implement.
brownover 15 years ago
The sentiment here seems to be overwhelmingly positive. Call me a cynic, but this reminds me of "magic_quotes" all over again: a "feature" that tries to help, but masks the fundamental problem.
评论 #1114103 未加载
thoraxover 15 years ago
<p><pre><code> That is, it is impossible to generate malformed webpages while using XHP. </code></pre> While the purist in me thinks this is great if everyone <i>else</i> uses it, the immense amount of productivity lost when I first started using kid templating (e.g. <a href="http://turbogears.org/about/kid.html" rel="nofollow">http://turbogears.org/about/kid.html</a> ) really burned me on this whole concept.<p>Sometimes I really do want to make a quick test page without crossing all my i's and dotting all the t's. Importing non-perfect markup from a designer is a big pain, too, in this kind of templating system.<p>And, though it's unfair to say so, some companies do well enough without 100% valid XML markup: <a href="http://blog.errorhelp.com/2009/06/27/the-highest-traffic-site-in-the-world-doesnt-close-its-html-tags/" rel="nofollow">http://blog.errorhelp.com/2009/06/27/the-highest-traffic-sit...</a>
评论 #1113879 未加载
jolieover 15 years ago
See also: Rasmus Lerdorf's discussion of XHP: <a href="http://toys.lerdorf.com/archives/54-A-quick-look-at-XHP.html" rel="nofollow">http://toys.lerdorf.com/archives/54-A-quick-look-at-XHP.html</a><p>"...when you combine XHP with HipHop PHP you can start to imagine that the performance penalty would be a lot less than 75% and it becomes a viable approach. Of course, this also means that if you are unable to run HipHop you probably want to think a bit and run some tests before adopting this."
stephenover 15 years ago
Scala does basically the same thing with XML:<p><a href="http://www.scala-lang.org/node/131" rel="nofollow">http://www.scala-lang.org/node/131</a><p>...although I doubt it does escaping by default. Should be simple enough to add while you're converting the scala.xml.NodeSeq (iirc) to text.<p>For an API that required both XML and JSON output, Scala's built-in XML support had us wishing the JSON version of the API was as easy as the XML version.
andreover 15 years ago
I'm going to give it a try, simply because of this: Facebook Lite site was written entirely with XHP.
whalesaladover 15 years ago
BTW for those of you interested in installing on Linux, you'll need php5-dev (so on deb/ubuntu machines a quick apt-get install php5-dev solves it). Run phpize from the root, then the normal ./configure, make, make install etc...
评论 #1114421 未加载
评论 #1114068 未加载
fookyongover 15 years ago
maybe I've misunderstood, but this seems to advocate mixing inline HTML and php logic - isn't that a huge step backwards in terms of web software architecture? I thought we were all using the MVC model by now...<p>My head just hurts thinking how utterly unmaintainable all that spaghetti code must be.
评论 #1114033 未加载
评论 #1114006 未加载
leftnodeover 15 years ago
Ugh, I dislike this a lot. I'm one of the guys who actually likes PHP, so this may be a bit skewed, but what's wrong with PHP's existing <i>alternate</i> syntax? Most people don't know about it, but it's clean and easy to follow:<p><pre><code> &#60;?php if ( true === $some_value ): ?&#62; &#60;div&#62;display this div&#60;/div&#62; &#60;?php else: ?&#62; &#60;div&#62;display this div instead&#60;/div&#62; &#60;?php endif; ?&#62; </code></pre> This way, you can keep basic logic in your templates (its inevitable and convenient), it's still PHP (there's endforeach, endfor, endwhile, etc.), and this method of templating is very clean.<p>You can now have a class that sets variables through __set(), loads up a .phtml file, starts output buffering, renders the file with those variables, and then returns it.<p>You can extend it further to automatically sanitize output variables for XSS and whatnot, cache output, etc. This way, you don't need some overly verbose system like smarty to do what PHP does already. XHP just looks like another smarty: solving a problem that I really don't think exists.<p>Edit: Ok, I probably shouldn't say I dislike this a lot, I do love seeing Facebook sticking with PHP and ultimately helping it out.
评论 #1115360 未加载
vlover 15 years ago
I'm using PHPTal right now to achieve more or less same effects, but it's not the easiest solution - it becomes a bit cumbersome for long fragments.<p>XHP looks very promising because it solves one of my problems with PHPTal - generating complex content in the loop (in PHPTal having multiple conditions in loop is possible, but not exactly elegant)<p>For example:<p><pre><code> &#60;?php $list = &#60;ul /&#62;; foreach ($items as $item) { if ($item-&#62;bold) { $list-&#62;appendChild(&#60;li&#62;&#60;b&#62;{$item}&#60;/b&#62;&#60;/li&#62;); } else if ($item-&#62;foobar) { $list-&#62;appendChild(&#60;li&#62;&#60;i&#62;{$item}&#60;/i&#62;&#60;/li&#62;); } else { $list-&#62;appendChild(&#60;li&#62;{$item}&#60;/li&#62;); } } ?&#62;</code></pre>
shaunxcodeover 15 years ago
Another awesome example of the lengths people will go to compensate for a lack of macros.<p>I would so much rather see (define table (html-table (map [tr (td _)] rows)))<p>then<p>$table = &#60;table&#62;; foreach($rows as $row) { $table-&#62;appendChild(&#60;tr&#62;&#60;td&#62;{$row}&#60;/td&#62;&#60;/tr&#62;); (assuming this is even possible?) }
评论 #1115982 未加载
justinphover 15 years ago
Sure, let's take two clusterfucks, php and xml, and smash them together! Great idea!<p>(Actually, this looks rather handy.. but there is a certain amount of initial WTF.)
drusenkoover 15 years ago
very, very cool. i can't wait to dig into this more... it looks like even the basic examples are cool, but there's lots of stuff under the hood waiting to be discovered.
评论 #1113896 未加载
thereover 15 years ago
seems to me that if they went to the trouble to make it understand xml syntax and error out on invalid code, they should have just made it auto-close tags. on pages where there are heavily nested divs and other things, auto-closing the tags would make the code smaller while still generating valid xhtml and not bothering the developer with such trivial things.<p><pre><code> echo &#60;div&#62;&#60;strong&#62;&#60;em&#62;blah;</code></pre>
评论 #1113909 未加载
评论 #1113940 未加载
callmeedover 15 years ago
This sure would make working with WordPress themes easier.
评论 #1113951 未加载
estover 15 years ago
looks like E4X, which security is a problem<p><a href="http://sla.ckers.org/forum/read.php?2,20408" rel="nofollow">http://sla.ckers.org/forum/read.php?2,20408</a><p>Any code mix operation &#38; data is dangerous. That's all how overflow exp, injection and XSS works