Skip to Main Content
Hack 与HHVM 权威指南
book

Hack 与HHVM 权威指南

by Owen Yamauchi
March 2017
Beginner content levelBeginner
247 pages
3h 55m
Chinese
China Machine Press
Content preview from Hack 与HHVM 权威指南
157
7
XHP
XHP
(类似
XHTML
命名方式)是
Hack
的一个特性,它允许编程人员借助于嵌入式
XML
类似的语法,把一个
HTML
树描述为
PHP/Hack
对象。在
Web
应用中,它消除了
整个种类的错误,以及安全漏洞的来源。它使得
UI
代码更整洁、更易于维护,也更加灵活。
在传统的
PHP
中,你在网页中输出可能通过下面两种方式之一,或者使用混合
HTML
PHP
模板:
<tt>
Hello
<strong>
<?= $user_name ?>
</strong>
!
</tt>
或者通过字符串连接或者字符串插入来输出:
echo
"<tt>Hello <strong>$user_name</strong>!</tt>";
有了
XHP
,上面的例子可能看起来是这样的:
echo
<tt>Hello <strong>{$user_name}</strong></tt>;
这是一个非常普通的
echo
语句,并且没有任何引号。这种类似
HTML
的语法是整个
XHP
语法体系中的一部分。
XHP
对于一个现代的、面向对象的
Web
应用
UI
库是一个非常好的基石。在本章的学习中,
我们将会看到你为什么应该使用它,怎么使用它,如何在它的基础上进行构建,还有如
何使用它对一个传统的代码库进行改造。
7.1
为什么使用
XHP
XHP
可以帮助提高你
UI
代码的安全性及正确性,它有着大量方法用于防止你犯常见的
158
7
错误。它还可以通过对你的
HTML
标记提供一个面向对象的接口,帮助你更稳健地组织
你的代码。
7.1.1
运行时验证
你能够发现以下这段代码中的问题吗? ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Reinventing the Organization for GenAI and LLMs

Reinventing the Organization for GenAI and LLMs

Ethan Mollick

Publisher Resources

ISBN: 9787111554844