1

如您所知,大多数 PHP 源代码不推荐插入结束标记,但我想知道为什么在Lithium 文档中,鼓励包含结束 PHP 标记?有什么合理的理由吗?除此之外,其文档中还有一些其他过时的样式。锂是一个活跃的项目吗?

更新:即使在仅包含 PHP 的文件中打开 php 标签似乎也是多余的,我们将其包含在内,因为目前我们必须这样做,(如果只有 php 代码的文件有标准文件扩展名,则无需打开标签),但更奇怪的是,甚至有人说我们应该包含一个结束标签!

4

2 回答 2

3

省略结束标签是草率的。

任何人这样做的唯一借口是您可能会不小心留下一些尾随空格,从而导致标题过早发送。这就是为什么这个理由完全是虚假的:

  1. 如果您安装了 Xdebug(如果您没有安装,则会遇到更大的问题),如果您尝试编写标头,它会准确地告诉您输出从哪里开始
  2. 我发现这种行为是整体缺乏对细节关注的表现;很有可能,如果你弄错了,你的代码也有其他问题(或者你有一个糟糕/配置不当的编辑器)
  3. 好的 QA 工具完全否定了这一点:我们有li3_quality,它是一个全面的代码质量验证器

从本质上讲,人们用最糟糕的懒惰来为马虎辩护。

如果您浏览 Lithium 的编码标准,您会注意到对对称性的高度关注。编码标准中的一切都是对称的。包括打开/关闭标签。这样做对我们来说从来都不是问题,因为我们拥有良好的 QA 工具。

于 2012-12-05T14:30:16.377 回答
2

Lithium Coding Standards

The link reffered to in the question ... Coding Standards ... is just that, what the Lithium devs believe are the best coding standards for their project. I personally agree that a closing tag is preferable.

While the PHP docs say it's "preferable" to not use the close tag if it's "pure" PHP code (mostly so you don't accidental add white space or something after it) a simple way to avoid that is to not do it in the first place. I prefer leaving in the tag so that ...

  • The code looks more uniform
  • To insure server compatibility
  • For better support of color-coding etc in text editors

This is also somewhat a misunderstanding of how Lithium works ... the examples given are for files that act as Controllers, Models, etc. These files are for the most part not directly accessed by end user requests like the Views are (well for lack of a simpler way to explain it.)

Is Lithium an active project?

As for if Lithium is an "active" project I suggest referring to their ...

As of this post the last dev branch commit was 10 hours ago.

Lithium's use of "short" tags ...

For the record, other examples in the Lithium doc's use the so called PHP "short" tags e.g. <?= ... ?> instead of: <?php ... ?> comes with an important caveat see the docs:

You might have noticed that the above example uses the short tag syntax to output the contents of a view variable. This syntax is a bit misleading, as Lithium does not depend on or use short tags: this output behavior works a bit differently from how it seems. When the view layer is rendered, each template is processed by a tokenizer before it is compiled into its final form. During this step something like this:

<?=$variable; ?>

Is translated into something like this:

<?php echo $h($variable); ?>

The $h() function you see used there escapes HTML. To make a long story short, this mechanism provides an easy way for you to make sure all dynamically-generated data is safely landing in your HTML template.

We highly recommend using the <?= ...; ?> syntax in your views, as it aids greatly in hardening your application against cross-site scripting (and related) attack techniques.

于 2012-12-05T06:27:54.917 回答