Skip to content
This repository has been archived by the owner on Apr 1, 2024. It is now read-only.

Increased usability of XHPHelpers, attribute coercion now throws exceptions

Compare
Choose a tag to compare
@fredemmott fredemmott released this 01 May 17:24
  • For classes using XHPHelpers, attributes are automatically transferred to the root element; for example, if you call $element->setAttribute('id', 'foo') and that element renders a div, that div will have the id attribute set. The class attribute is appended instead of replacing the existing value
  • In line with the rest of Hack and the increased typechecker strictness in HHVM 3.7, incorrect attribute types now throw an exception instead of logging an E_USER_DEPRECATED
  • This release also contains the change to the 2.0.x branch reducing the boilerplate for XHPAsync - see the v2.0.2 release notes for details

XHPHelpers Example

class :foo extends :x:element {
  use XHPHelpers;

  attribute :xhp:html-element;

  protected function render(): XHPRoot {
    return <div />;
  }
}

// v2.0.x: <div></div>
// v2.1.x: <div id="bar"></div>
var_dump((<foo id="bar" />)->toString());

Attribute Types Example

class :foo extends :x:element {
  attribute int bar @required;
  // ...
}

// Typechecker error in HHVM 3.7+
// E_USER_DEPRECATED in XHP-Lib 2.0.x
// Exception in XHP-Lib 2.1+
$x = <foo bar="123" />; 

// Typechecker error in HHVM 3.7+
// E_USER_DEPRECATED in XHP-Lib 2.0.x
// Exception in XHP-Lib 2.1+
$x = <foo bar={"123"} />;

// Correct
$x = <foo bar={123} />;

You can revert to the previous behavior as follows:

XHPAttributeCoercion::SetMode(XHPAttributeCoercionMode::LOG_DEPRECATION);

This will not remove the typechecker errors, and we expect to remove this option in v3.