I’m not sure if I’m losing it or what. I recently jumped back into PHP after a much needed break, and I’m trying to do something that I’ve always been able to do: call a public class method without instantiating the class. Example:
class Utils
{
public function getTime()
{
return time();
}
}
$time = Utils::getTime();
echo $time;
I used to do this all the time (about two or three years ago), but after hopping into PHP 5.3 on a new sandbox environment that I set up, I keep getting
Fatal error: Call to undefined function getTime() in /mnt/richard/index.php on line 24
Am I missing something silly here? Or is the use of public class methods without class instantiation a now deprecated feature in PHP? Oh how times have changed…
My overall goal is to be able to create methods that belong to a grouped set of classes that can be called in the global scope within other methods and classes. Any help will be much appreciated. Thanks.
You shouldn’t be calling instance methods as static ones on a class, even if PHP allows you to do so. When invoking:
you are calling an instance method from a static context. You should define
getTimelike this instead: