无法从另一个类中调用静态方法
我有一个用户类和一个日志类。在 Users 类中,我有几种方法。如果在方法中发现错误,我会调用 Log 类中的静态方法,将错误写入文本文件和数据库。但是,尝试实际调用静态方法不起作用,并且我没有收到错误消息。
我通过使用“require_once 'Log.php'”将 Log 类包含在 Users 类中,并使用 Log::log_error() 调用该方法。
那么这是怎么回事呢?
I have a Users class and a Log class. Within the Users class I have several methods. If there is an error found inside a method, I make a call to a static method within the Log class to write the error to a text file and database. However, trying to actually call the static method doesn't work and I'm not getting an error message.
I am including the Log class in the Users class by using 'require_once 'Log.php' and calling the method by using Log::log_error().
So what's going on here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我调用静态方法的方式没有任何问题;我的程序中出现语法错误(缺少分号)。经过一晚良好的睡眠后,我意识到自己是个多么愚蠢的人,并决定运用我的大脑和常识。对于其他新手,这就是我所做的:
我确保将错误报告设置为 error_reporting(E_ALL) (注意:这不是生产服务器);
然后,我搜索了错误日志(RHEL5 系统上的 */var/log/http/error_log*)并运行命令 tail -f 来实时查看新的日志条目。
然后我再次运行该脚本,果然出现了一个不错的“PHP 解析错误:语法错误..”错误。
作为参考,如果任何不熟悉 OOP 的人想了解我如何使用静态方法调用,这里有一个代码片段:
There was nothing wrong with the way I was calling the static method; I just had a syntax error in my program (missing semi-colon). After a good nights sleep, I realized what a prat I was being and decided to use my brain and common sense. For other noobs, here's what I did:
I made sure I had errors reporting set to error_reporting(E_ALL) (Note: this is NOT a production server);
I then searched for my error logs (*/var/log/http/error_log* on my RHEL5 system) and ran the command tail -f to see new log entries in real time.
I then ran the script again and sure enough I had a nice "PHP Parse error: syntax error.." error.
For reference, in case anyone who is not familiar with OOP wants to see how I was using a static method call, here's a code snippet: