查找 PHP(和/或 SMARTY)中定义变量的位置?
我目前正在做一个非常大的项目,为了尽快完成它面临很大的压力,而且我遇到了一个严重的问题。以一种非常奇怪的方式编写最后定义的变量的程序员 - 配置变量并不都在同一个文件中,它们分布在超过 500 个文件和 10 万多行代码的整个项目中,我有弄清楚某个变量在哪里,这样我就可以解决问题了。
有没有办法追踪这个变量?我相信他正在使用 SMARTY (由于类似的问题,我无法忍受),并且该变量是模板变量。我相当确定我正在寻找的变量最初被定义为 PHP 变量,然后该变量被传递到 SMARTY 中,所以我想追踪 PHP 变量,但是如果这是不可能的 - 我该如何追踪他在哪里为 SMARTY 定义了变量?
PS 我在 Vista 中,并且没有 ssh 访问服务器的权限,所以“grep”是不可能的。
I'm currently working on a very large project, and am under a lot of pressure to finish it soon, and I'm having a serious problem. The programmer who wrote this last defined variables in a very odd way - the config variables aren't all in the same file, they're spread out across the entire project of over 500 files and 100k+ lines of code, and I'm having a hell of a time figuring out where a certain variable is, so I can fix an issue.
Is there a way to track this variable down? I believe he's using SMARTY (Which I can not stand, due to issues like this), and the variable is a template variable. I'm fairly sure that the variable I'm looking for was initially defined as a PHP variable, then that variable is passed into SMARTY, so I'd like to track down the PHP one, however if that's impossible - how can I track down where he defined the variable for SMARTY?
P.S. I'm in Vista, and don't have ssh access to the server, so 'grep' is out of the question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
暴力方式,因为有时聪明的变量不是直接分配的,但它们的名称可以存储在变量中,由许多字符串连接或某些函数的结果,这使得无法通过简单的搜索/greping在文件中找到。
首先,编写自己的函数来打印可读的回溯,即:
打开主 smarty 文件(默认为
Smarty.class.php
),在第 580 行左右有一个名为 assign 的函数。修改它以监视所需的变量名称:第二个函数可能需要进行相同的修改 - assign_by_ref。现在,运行脚本后,您应该得到如下输出:
第二行指向首次分配变量的位置。
Brute force way, because sometimes smarty variables are not directly assigned, but their names can be stored in variables, concatenated from many strings or be result of some functions, that makes it impossible to find in files by simply searching / greping.
Firstly, write your own function to print readable backtrace, ie:
Open main smarty file (
Smarty.class.php
by default) and around line 580 there is function called assign. Modify it to watch for desired variable name:The same modification may be required for second function - assign_by_ref. Now after running script you should have output like that:
Second line points to the place where variable was first assigned.
这类事情是我在所有 Windows 计算机上安装 Cygwin 的第一大原因。
我无法想象没有有效的
grep
的编程。This sort of thing is the #1 reason I install Cygwin on all my windows machines.
I can't imagine programming without a working
grep
.还有一个有趣的进一步选择,虽然很难看,但如果你真的迷路了,它会很有帮助。
如果您想知道
THE_NAME
的定义位置,请在您确定首先运行的位置写下这样的行:如果稍后 PHP 将运行您要查找的定义,它将写出如下通知这样:
那么您就知道您要查找的定义位于文件
SomeConfiguration.php
的第 89 行中。要使其正常工作,您必须考虑
因此有时添加一些
exit('here')
以避免模糊输出会有所帮助。也许您必须缩小范围,或者必须提前设置error_reporting
,但您会找到它。There is an interesting further option, ugly like hell but helpful if you are really lost.
If you would like to know where
THE_NAME
was defined, write lines like these on a place you are sure is run first:If later PHP will run the definition you are looking for, it will write a notice like this:
Then you know that the definition you are looking for is in the file
SomeConfiguration.php
on line 89.To have this working, you must consider
So sometimes it helps to add some
exit('here')
in order not to blur the output. Maybe you have to narrow down a bit or you have to seterror_reporting
earlier, but you'll find it.这不是一个完美的解决方案,但我发现 agent ransack 很有用用于搜索大型目录和文件。可能会帮助您缩小范围。搜索结果将允许您阅读在结果窗格中找到匹配项的确切行。
It's not a perfect solution, but I find agent ransack useful for searching large directories and files. Might help you narrow things down. The search results will allow you to read the exact line it finds a match on in the result pane.
如果您使用 netbeans 编辑器,只需“右键单击”-> “转到定义”
或
ctrl + 单击
变量。如果编辑器无法弄清楚,您可以回退到“在文件中查找”选项。
If you use the netbeans editor just "right click" -> "go to Definition"
Or
ctrl + click
on the variable.If the editor can't figure it out, you could fallback to the "Find in files" option.
只需使用可用的 PHP IDE 之一(如果您真的很绝望,也可以使用像 Notepad++ 这样的简单文本编辑器)并在所有源文件中搜索变量的名称(大多数 PHP IDE 还支持查找函数/变量的定义位置并允许您可以跳转到相关的代码段)。虽然你不知道哪一段代码调用了模板,这看起来很奇怪(无论是 Smarty 还是其他任何东西并不重要)。您应该能够从 URI 开始深入查看代码(使用任何支持调试的 IDE),因为这样您就一定会看到所述变量的定义位置。
Just use one of the available PHP IDEs (or a simple text editor like Notepad++ if you're really desperate) and search for the name of the variable in all source files (most PHP IDEs also support finding where functions/vars were defined and allow you to jump to the relevant piece of code). Though it seems weird that you don't know what piece of code calls the template (whether it's Smarty or anything else doesn't really matter). You should be able to drill down in the code starting from the URI (using any IDE which supports debugging), because that way you're bound to see where said variable is defined.