简体   繁体   English

解决PHP解析错误

[英]Hunting down PHP parse errors

When writing PHP code, you can cause some really hard to track down parsing errors if you don't remember what parts you were editing previously. 编写PHP代码时,如果您不记得之前编辑的部分,可能会导致某些人难以追踪解析错误。 I'd be interested to hear about any insightful methods to hunt down these errors you may have discovered. 我很想知道任何有见地的方法来找出你可能发现的这些错误。

Example: unexpected ';', expecting T_FUNCTION in someclass.php on line 877 示例:意外';',在第877行的someclass.php中期待T_FUNCTION

This vague error points out that you have unexpected ; 这个模糊的错误指出你有意外; in a class containing 877 lines of PHP code, and the error definitely is not on the last line. 在包含877行PHP代码的类中,错误肯定不在最后一行。 Is the error message misleading? 错误消息是否具有误导性? Where and how do you start looking, besides starting from the top and just trying to find the offending place scanning every row of code. 你从哪里以及如何开始寻找,除了从顶部开始,只是试图找到扫描每一行代码的有问题的地方。

First of all, I'd go using an editor that highlight PHP errors on the fly, when you type your code, like, for instance, Eclipse PDT (which is quite powerful, actively maintained, and free, and OSS) -- it will help detect some errors almost immediately, without having to execute the code. 首先,当你输入代码时,我会使用一个动态突出显示PHP错误的编辑器,例如, Eclipse PDT (功能强大,主动维护,免费,OSS) - 它将帮助几乎立即检测到一些错误,而无需执行代码。

If you are using it along with it's Subversion plug-in (to integrate SVN access in Eclipse), it can also display what it calls "quick diff" : the margin of modified lines which have not been committed to SVN is highlighted -- it help detecting what you changed since the last commit. 如果你将它与它的Subversion插件一起使用(在Eclipse中集成SVN访问),它还可以显示它所谓的“快速差异”:未被提交到SVN的修改行的边界被突出显示 - 它帮助检测自上次提交后您更改的内容。


Note, though, that, as it's based on Eclipse, requires a quite powerful computer (I'd say a dual-core with 2GB RAM is required, 1 GB generally being not enough if you also want to use some other software at the same time ^^ ) 但是请注意,因为它基于Eclipse,需要一台功能非常强大的计算机(我想说需要一个2GB内存的双核,如果你也想在同一个地方使用其他软件,1 GB通常是不够的时间^^)


Then, when you have being programming in PHP for quite some time, you'll probably be able to understand those messages faster / better, and will know where to look ;-) 然后,当您在PHP中编程很长一段时间后,您可能能够更快/更好地理解这些消息,并且知道在哪里查看;-)

如果您使用的是某些控件版本系统,则可以查找文件/类的实际版本和旧版本之间的差异。

这类错误通常是if,switch,while,do,function,class等的“{”未关闭。

You can also do a frequent syntax check, no matter what editor you use: 无论使用何种编辑器,您还可以进行频繁的语法检查:

php -l file.php

Note that I use the word "frequent". 请注意,我使用“频繁”一词。 If you use vim, you may find the following useful in your .vimrc file: 如果您使用vim,您可能会在.vimrc文件中找到以下内容:

map <F12> <ESC>:!php -l %<CR>

Just hit F12 at any time to check syntax on the fly. 只需按F12即可随时查看语法。

Run code as frequently as possible as you write it. 在编写代码时尽可能频繁地运行代码。 The fewer lines of code you add between each execution, the smaller the search space for new errors. 每次执行之间添加的代码行越少,新错误的搜索空间就越小。

If I've only edited 5 lines of the 877 line file since the last time I loaded the page, finding the error is likely much faster than if I've edited 100 lines. 自从我上次加载页面以来,我只编辑了5行的877行文件,找到错误的速度可能比我编辑100行时快得多。

I find PHP's reporting on syntax errors to be pretty good, but there is obviously limitation to what is possible, given that a syntax error can be almost anything. 我发现PHP关于语法错误的报告非常好,但是由于语法错误几乎可以是任何东西,因此可能存在明显的限制。 In general, your error is probably close to the line reported. 通常,您的错误可能接近报告的行。 For example, in this case, I would assume that you have done something like: 例如,在这种情况下,我会假设你做了类似的事情:

class Foo {
  function blah() {
  };
}

or perhaps: 也许:

class Foo {
  protected $foo;;
}

The semicolon is illegal in both of those contexts. 在这两种情况下,分号都是非法的。

Zend Studio has a great debugging tool that allows you to a variety of options including setting breakpoints, stepping through your code, and inspecting variables / parameters. Zend Studio有一个很棒的调试工具,允许您使用各种选项,包括设置断点,单步执行代码以及检查变量/参数。 Ofcourse, all of this comes with a price. 当然,所有这些都需要付出代价。 =( =(

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

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