Bad Programmer
Bad Programmer

Reputation: 3752

Can't Call Static Method From Within Another Class

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?

Upvotes: 0

Views: 7432

Answers (1)

Bad Programmer
Bad Programmer

Reputation: 3752

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);
     }
  }

}

Upvotes: 5

Related Questions