Operators: XHP Attribute Access

XHP is such a big topic that it is discussed in detail elsewhere, but a special operator (->:) is provided for XHP in order to access the attributes of an XHP object:

// Without the operator:
$variable = $xhp_object->getAttribute('attributeName');
// With the operator:
$variable = $xhp_object->:attributeName;

As well as making the code more concise, the typechecker understands the operator and is able to make sure the attribute value is used in a type-safe manner:

<?hh

// For XHP library, etc.
class :comparison:greeting extends :x:element {
  attribute string first = "Good";
  attribute string last = "Afternoon";

  public function render(): XHPRoot {
    // Using getattribute()
    $first = $this->getAttribute("first");
    // using ->:
    $last = $this->:last;

    $ret = $first.$last;

    /* To the typechecker:
     *
     * - $first has an unknown type, because the return type of getAttribute()
     *   varies.
     * - $last is a string because Hack understands that the ->: operator is
     *   fetching the 'last' attribute, which is in turn a string.
     * - $ret is a string because it is the result of concatenation.
     */
    return <x:frag>{$ret}</x:frag>;
  }
}

echo <comparison:greeting />;
Output
GoodAfternoon

Note that this operator cannot be used in a write context. That is, the following is an error:

$xhp_object->:attributeName = 1;

We may implement support for this in the future; in the mean time, you need to use the setAttribute() method (which is not typechecked).

Namespaces

Don't use Namespaces

Currently, XHP doesn't handle namespaces very well. So, your XHP code should generally be in the global namespace. This problem is known.