Unable to execute static method - FATAL ERROR

I have been working on a PHP web application. I used the new datagrid from gurrido.net and it worked fine on the local computer, but when I upload it to the server, I get the following error:

Fatal error: Unable to create the static method Base :: getClassName () static in the Singletons class in /var/www/reskb/phpinc/Singletons.class.php on line 84

In my old version, where I did not use the grid, I got it working. Here is my singletons.class.php file code:

<? class Singletons extends Base { var $objects = array(); function getClassName() { return 'Singletons'; } function _instance() { static $_instance = NULL; if ($_instance == NULL) { $className = Singletons::getClassName(); $_instance = new $className(); } return $_instance; } function put($object) { $self = Singletons::_instance(); $className = $object->getClassName(); $self->objects[$className] = $object; } function get($className) { $self = Singletons::_instance(); if(!empty($self->objects[$className])) return $self->objects[$className]; else return ''; } } Singletons::_instance(); ?> 
+4
source share
3 answers

You must call the getClassName function with an object or define getClassName as static. -

  <?php class Singletons extends Base { var $objects = array(); static function getClassName() { return 'Singletons'; } static function _instance() { static $_instance = NULL; if ($_instance == NULL) { $className = Singletons::getClassName(); $_instance = new $className(); } return $_instance; } function put($object) { $self = Singletons::_instance(); $className = $object->getClassName(); $self->objects[$className] = $object; } function get($className) { $self = Singletons::_instance(); if(!empty($self->objects[$className])) return $self->objects[$className]; else return ''; } } Singletons::_instance(); ?> 
+1
source

You get an error because you are trying to call your function statically when it is not a static method (function).

You need to specify the function as static:

 static function getClassName() { return 'Singletons'; } 

This applies to all methods that you want to call statically.

0
source

If you declare a function as abstract in an abstract superclass, and then try to define it as static in a child class, you will receive a fatal error. You might want to think about how your class hierarchy is structured, since the only way is to remove the abstract function declaration from the superclass.

0
source

All Articles