无法制作非静态方法 - 致命错误

时间:2021-09-04 22:46:23

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

我正在开发一个PHP Web应用程序。我使用了来自gurrido.net的新数据网格,它在本地运行良好但是当我将其上传到服务器时,我收到以下错误:

Fatal error: Cannot make non static method Base::getClassName() static in class Singletons in /var/www/reskb/phpinc/Singletons.class.php on line 84

致命错误:无法在第84行的/var/www/reskb/phpinc/Singletons.class.php中的单例类中使用非静态方法Base :: getClassName()static

In my old version where I didn't use the grid, I got it working. Here is my code of singletons.class.php file:

在我没有使用网格的旧版本中,我得到了它的工作。这是我的singletons.class.php文件的代码:

<?
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();
?>

2 个解决方案

#1


1  

You should call function getClassName using object or define getClassName as static. –

您应该使用object调用函数getClassName或将getClassName定义为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();
    ?>

#2


0  

You're getting the error because you're trying to call your function statically when it isn't a static method (function).

您收到错误是因为当它不是静态方法(函数)时,您试图静态调用函数。

You need to specify the function as static:

您需要将该函数指定为static:

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

That goes for every method you'd like to call statically.

这适用于您希望静态调用的每种方法。

#1


1  

You should call function getClassName using object or define getClassName as static. –

您应该使用object调用函数getClassName或将getClassName定义为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();
    ?>

#2


0  

You're getting the error because you're trying to call your function statically when it isn't a static method (function).

您收到错误是因为当它不是静态方法(函数)时,您试图静态调用函数。

You need to specify the function as static:

您需要将该函数指定为static:

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

That goes for every method you'd like to call statically.

这适用于您希望静态调用的每种方法。