不应静态调用非静态方法 AJAXChatFileSystem::getFileContents()

Non-static method AJAXChatFileSystem::getFileContents() should not be called statically

我正在使用 PHP 7 在我的 Apache 服务器 (2.4) 中设置 Ajax-Chat,但出现此错误

Deprecated: Non-static method AJAXChatFileSystem::getFileContents() should not be called statically in C:\Apache24\htdocs\services\chat\lib\class\AJAXChatTemplate.php on line 37

我尝试将 function getContent() 更改为 public static function getContent() 但之后显示:

Fatal error:Uncaught Error: Using $this when not in object context in C:\Apache24\htdocs\services\chat\lib\class\AJAXChatTemplate.php:36

class AJAXChatTemplate {

    var $ajaxChat;
    var $_regExpTemplateTags;
    var $_templateFile;
    var $_contentType;
    var $_content;
    var $_parsedContent;

    // Constructor:
    function __construct(&$ajaxChat, $templateFile, $contentType=null) {
        $this->ajaxChat = $ajaxChat;
        $this->_regExpTemplateTags = '/\[(\w+?)(?:(?:\/)|(?:\](.+?)\[\/))\]/se';
        $this->_templateFile = $templateFile;
        $this->_contentType = $contentType;
    }

    function getParsedContent() {
        if(!$this->_parsedContent) {
            $this->parseContent();
        }
        return $this->_parsedContent;
    }

     function getContent() {
        if(!$this->_content) {
            $this->_content = AJAXChatFileSystem::getFileContents($this->_templateFile);
        }
        return $this->_content;
    }
}

在 PHP 中静态调用非静态方法是 deprecated behaviour since version 7.0 并引发 E_DEPRECATED 警告。这意味着对这种行为的支持有效,但可能(并且很可能会)在未来的版本中被删除。

此行为在 PHP 版本 5.*.

中引发了 E_STRICT 警告

将您自己的 AJAXChatTemplate::getContent() 方法更改为静态方法不起作用,因为它使用 $this,这仅在 class 的实例上下文中才有意义。因此它会在静态上下文中触发致命错误。

您正在使用讨论您遇到的错误的AJAX-Chat library—you haven't stated what version you are using but there is an issue

根据这个报告的问题,a commit 这个库的最新版本是为了改变这个静态行为。


要解决您的问题,您有两种选择:

继续使用您当前安装的 AJAX-Chat 版本

只需非静态地使用 AJAXChatFileSystem::getFileContents()。创建 class 的实例并通过修改 getContent() 方法来使用它,如下所示:

function getContent()
{
    if (!$this->_content) {
        $ajaxChatFileSystem = new AJAXChatFileSystem(); 
        $this->_content = $ajaxChatFileSystem->getFileContents($this->_templateFile); 
    }

    return $this->_content;
}

升级本库到最新版本,使用静态方法

似乎没有更新日志,因此您应该在使用 AJAX-Chat 的任何地方测试您的代码,以确保没有重大更改。


从技术上讲,您还有第三种选择:由于这是一个 E_DEPRECATED 警告——暗示该功能被标记为在将来删除——您可以安全地忽略此警告,现在.

E_DEPRECATED 警告(与所有通知、警告和错误一样)应禁止在生产代码中向用户显示。

但是,我推荐这个,因为你的日志将充满E_DEPRECATED警告。此外,如前所述,PHP 的未来版本可能会删除对静态调用非静态方法的支持。

希望这对您有所帮助:)