无法从另一个类中调用静态方法

发布于 2024-12-29 07:21:40 字数 215 浏览 0 评论 0原文

我有一个用户类和一个日志类。在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北城挽邺 2025-01-05 07:21:40

我调用静态方法的方式没有任何问题;我的程序中出现语法错误(缺少分号)。经过一晚良好的睡眠后,我意识到自己是个多么愚蠢的人,并决定运用我的大脑和常识。对于其他新手,这就是我所做的:

我确保将错误报告设置为 error_reporting(E_ALL) (注意:这不是生产服务器);

然后,我搜索了错误日志(RHEL5 系统上的 */var/log/http/error_log*)并运行命令 tail -f 来实时查看新的日志条目。

然后我再次运行该脚本,果然出现了一个不错的“PHP 解析错误:语法错误..”错误。

作为参考,如果任何不熟悉 OOP 的人想了解我如何使用静态方法调用,这里有一个代码片段:

class Log
{
   public static function log_err($data)
   {
     //put code here
   }
}


class User
{
  private function user_action($action)
  {
     //put code here

     //If error call static method from Log class
     if($err)
     {  
        Log::log_err($data);
     }
  }

}

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:

class Log
{
   public static function log_err($data)
   {
     //put code here
   }
}


class User
{
  private function user_action($action)
  {
     //put code here

     //If error call static method from Log class
     if($err)
     {  
        Log::log_err($data);
     }
  }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文