为什么 Symfony 建议关闭 php_short_tags
我第一次安装 Symfony2 框架,Web 配置屏幕显示要关闭 php 短标签。除了与 xml 冲突或服务器不兼容的参数之外,还有其他原因吗?有 Symfony 特有的问题吗?
I'm installing Symfony2 framework for the first time and the web config screen says to turn php short tags off. Is there any reason for this besides the arguments that in conflicts with xml or server incompatibility? Any issues specific to Symfony?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一般来说,使用
short_tags
绝不 是一个好主意,主要是因为服务器不兼容。服务器不兼容性
并非每个 PHP 服务器都启用了
short_tags
。因此,如果您在项目中的任何位置使用短标签,并且将来的某一天它碰巧被放置在禁用短标签的服务器上,那么您的网站现在就损坏了。您必须检查代码并查找/替换或在服务器上启用短标记,无论如何您可能无权执行此操作。Symfony
Symfony 提出了一些最佳实践建议。它们不一定会决定 Symfony 开发的成败,但它们是许多专业开发人员遵循的良好实践。不使用
short_tags
是一件值得付诸实践的好事情。不管怎样,所有短标签真正所做的就是让您不必时不时地输入一些额外的字符。如果您真的很担心输入额外的字符,那么您确实应该使用支持片段的 IDE 或文本编辑器,例如 Sublime Text 。
In general, it's never a good idea to use
short_tags
mostly because of server incompatibility.Server Incompatibility
Not every PHP server has
short_tags
enabled. So if you use short tags anywhere in a project, and someday in the future it happens to be put on a server with short tags disabled, your site is now broken. You have to either go through your code and find/replace or enable short tags on the server, which you might not have the authority to do anyways.Symfony
Symfony makes several Best Practice suggestions. They are not necessarily things that will make or break development using Symfony, but they are good practices that many professional developers follow. Not using
short_tags
is a good thing to put into practice.Anyways, all short tags really does is save you from having to type a few extra characters every now and then. If you are really that concerned about typing extra characters, you should really be using an IDE or Text Editor that supports snippets, like Sublime Text.
恕我直言,如果您愿意,请随意启用 Short_open_tag,并且可以安全地忽略 Symfony 发出的此警告。哎呀,我建议完全取消支票。
您引用的两个可能的问题是您在使用短开放标记时可能遇到的唯一理论上问题,但实际上,在我开发 PHP 应用程序的 10 年中,这对我来说从来都不是问题。
即使在 XML 密集型应用程序中,PHP 也不可能在多个位置解析 XML 标头,并且在少数情况下,可以通过例如回显 XML 标头本身来轻松规避问题。在这个后 XHTML 时代,您更不可能在 PHP 模板中开发任何带有 XML 标头的 Web 应用程序。首先是一个毫无意义的争论。
就服务器配置而言,即使是普通的共享虚拟主机也允许修改 PHP 配置,特别是此设置甚至可以在运行时以您无法设置的远程方式进行设置。到底是谁将 Symfony 应用程序部署到共享主机上?
短的开放标签需要一些爱,他们在 PHP 5.4 中得到了一些爱,其中 echo 语法 (
>
) 将被启用,无论 Short_open_tag 设置如何。此外,尽管有些人可能会声称,短标签绝不是一个已弃用的 PHP 功能,并且会继续存在。如果您使用 PHP 模板,并且更喜欢短标签语法,那就去做吧!
IMHO, feel free to enable short_open_tag if you prefer, and it is safe to ignore this warning issued by Symfony. Heck, I'd advocate removing the check entirely.
The two possible problems you cite are the only theoretical problems you might encounter from using short open tags, but in practice this never has been an issue to me in my 10 years developing PHP applications.
Even in XML-heavy application, it is unlikely you have an XML header parsed by PHP in more than a very few locations, and in those few occasions the problem is easily circumvented, by for example echoing the XML header itself. In this post-XHTML era, it is even more unlikely you'll be developing any web application with an XML header in a PHP template. A moot argument to begin with.
As far as the server configuration goes, even the average shared webhosting allows the PHP configuration to be modified these days, and this setting in particular can even be set at runtime in the remote possibility you can't. And who's deploying Symfony applications to a shared hosting anyway?
Short open tags need some loving, and they are getting some in PHP 5.4, where the echo syntax (
<?= ?>
) will be enabled regardless of the short_open_tag setting. Also, despite of what some might claim, short tags are in no way a deprecated PHP feature, and is here to stay.If you use PHP templates, and prefer the short tags syntax, go for it!
AFAIK,如果您使用短标签,Symfony 1 或 2 中没有任何特定的内容会破坏,但从个人经验来看,它们不值得为您节省少量的打字量。您提到了两个有效的投诉 - XML 冲突和服务器不兼容。这些足以成为不使用它们的理由。将项目从一台服务器移动到另一台服务器并必须替换短标签是一种令人讨厌的时间浪费:)
AFAIK, there's nothing specific in Symfony 1 or 2 that will break if you use short tags, but speaking from personal experience, they're not worth the small amount of typing they save you. You mention two valid complaints - XML conflicts and server incompatibility. Those are reason enough not to use them. Moving projects from one server to another and having to replace short tags is an annoying waste of time :)