简体   繁体   English

在Lithium中关闭PHP标签

[英]Closing PHP tag in Lithium

As you know most PHP sources deprecated inserting closing tag, but I wonder why in Lithium documentation , it is encouraged to include a closing PHP tag? 如您所知,大多数PHP源文件都不赞成插入结束标记,但是我想知道为什么在Lithium文档中 ,鼓励它包含结束PHP标记? Is there any rational reason for that? 有什么合理的理由吗? Beside that there is some other obsolete style in its documentation. 除了它的文档中还有其他一些过时的样式。 Is Lithium an active project? 锂是一个活跃的项目吗?

Update: Even opening php tag in a file that contains only PHP seems redundant and we include it because currently we have to do that, (if there was a standard file extension for files that have only php code no need for opening tag remained), but more odd is that somebodies even say that we should include a closing tag! 更新:即使在仅包含PHP的文件中打开php标签似乎也是多余的,我们将其包含在内,因为当前我们必须这样做(如果只有php代码的文件具有标准文件扩展名,则无需打开标签),但更奇怪的是,甚至有人说我们应该添加一个结束标记!

Omitting closing tags is sloppy. 省略结束标记是草率的。

The only excuse anyone ever gives for doing it is that you might accidentally leave some trailing whitespace, causing headers to be sent prematurely. 任何人这样做的唯一借口是,您可能会不小心留下一些尾随空格,从而导致标题过早发送。 Here's why this justification is completely bogus: 这就是为什么这种辩解完全是伪造的:

  1. If you have Xdebug installed (and if you don't, you have bigger problems), it'll tell you exactly where your output has started if you try to write a header 如果您安装了Xdebug(如果没有安装,则会遇到更大的问题),如果您尝试编写标头,它会告诉您输出的确切位置
  2. I find this kind of behavior symptomatic of an overall lack of attention to detail; 我发现这种行为表现为对细节的整体缺乏关注。 chances are, if you got this wrong, there are other things wrong with your code as well (or you have a shitty/poorly-configured editor) 很有可能,如果您错了,那么代码也可能有其他问题(或者您的编辑器配置不正确)
  3. This is completely negated by good QA tooling: we have li3_quality , which is a comprehensive code quality validator 好的QA工具完全否定了这一点:我们拥有li3_quality ,这是一个全面的代码质量验证器

Essentially, people are justifying sloppiness with the worst form of laziness. 本质上,人们以懒惰的形式来证明草率。

If you browse through Lithium's coding standard, you'll notice the high level of attention paid to symmetry. 如果浏览Lithium的编码标准,您会注意到对对称性的高度关注。 Everything in the coding standard is symmetrical. 在编码标准一切都是对称的。 Including open/close tags. 包括打开/关闭标签。 Doing so is never a problem for us, because we have good QA tooling. 这样做对我们来说从来都不是问题,因为我们拥有良好的质量检查工具。

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. 虽然PHP文档说,如果它是“纯” PHP代码,则最好不使用close标记(多数情况下,不要意外在其后添加空格或其他内容),这是避免这种情况的一种简单方法,那就是不要这样做第一名。 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.) 这也对Lithium的工作方式造成了某种误解……给出的示例是针对充当ControllersModels等文件的。这些文件在大多数情况下不会像Views一样被最终用户请求直接访问(由于缺乏一种更简单的解释方式。)

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. 截至本文发布时,最后一次dev分支提交是在10个小时前。

Lithium's use of "short" tags ... 锂对“短”标签的使用...

For the record, other examples in the Lithium doc's use the so called PHP "short" tags eg <?= ... ?> instead of: <?php ... ?> comes with an important caveat see the docs : 作为记录,Lithium文档中的其他示例使用所谓的PHP“短”标签,例如<?= ... ?>代替: <?php ... ?>附带了一个重要的警告,请参阅文档

You might have noticed that the above example uses the short tag syntax to output the contents of a view variable. 您可能已经注意到,上面的示例使用short标记语法来输出view变量的内容。 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. 该语法有点误导,因为Lithium不依赖或不使用短标签:此输出行为的工作方式与外观有所不同。 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. 您在此处使用的$h()函数转义了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. 简而言之,此机制为您提供了一种简单的方法,以确保所有动态生成的数据都可以安全地放到HTML模板中。

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. 在您的视图中使用<?= ...; ?>语法,因为它有助于极大地增强您的应用程序免受跨站点脚本(和相关)攻击技术的侵害。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM