How It Works

scottmac edited this page Sep 13, 2010 · 2 revisions

This article is useful to hackers who are interested in how XHP works under the hood.

The XHP PHP extension which hooks into compile_file and compile_string. XHP will intercept calls to these functions and run all the code through a preprocessing step. The XHP compilation stage parses a superset of PHP and then returns code that PHP can understand. Therefore, all XHP really gives you is syntatic sugar via automatic metaprogramming.

Class Name Construction

Leading-colon class names are munged into @T_LABEL@’s that PHP can understand (since XHP accepts hyphens and colons in an element name, which are not valid in class names). The rule here is that - will become _ and : will become __. The constructed class name will then be prepended with xhp_. So, class :fb:thing-container {} will become class xhp_fb__thing_container {} when PHP sees it. Note that this encoding should not be depended on, and may change in the future.

Element Instantiation

Element construction by the XHP XML syntax is rewritten to nested new operators. XHP assumes that every element is an object whose constructor takes two arguments. The first argument is a dictionary (array) of attributes, and the second argument is a list (array) of children. Entities in XHP are decoded at compile time to their raw equivalency; & becomes & in the children list. These are then re-encoded back to entities at run-time.

attribute, children, and category Keywords

These keywords are rewritten to functions __xhpAttributeDeclaration, __xhpChildrenDeclaration, and __xhpCategoryDeclaration. Each of these functions returns some representation of the declaration. These can be fairly hairy, so you’re probably better off just looking at the PHP code which calls each of these functions.


PHP currently only allows you to use the [] operator on variables. This is just an unneeded restriction so I figured while I was working with PHP’s syntax that I might as well solve this. In XHP when you use the [] operator on something that would be a syntax error in vanilla PHP it will rewrite it into a call to __xhp_idx(). For instance, foo()['bar'] will become __xhp_idx(foo(), 'bar'). __xhp_idx is a C function defined in the PHP extension which behaves identically to the [] operator.

Note: This feature is off by default. To enable it, see Configuration.

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.