I am setting up Ajax-Chat in my Apache Server (2.4) with PHP 7 but I am getting this error
Deprecated: Non-static method AJAXChatFileSystem::getFileContents() should not be called statically in C:\Apache24\htdocs\services\chat\lib\class\AJAXChatTemplate.php on line 37
I tried changing function getContent()
to public static function getContent()
but after that it's showing:
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+?)(?:(?:\/)|(?:\](.+?)\[\/\1))\]/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;
}
}
Calling a non-static method statically in PHP is deprecated behaviour since version 7.0 and raises an E_DEPRECATED
warning. This means that support for this behavour works, but may (and probably will) be removed in a future version.
This behaviour raised an E_STRICT
warning in PHP versions 5.*.
Changing your own AJAXChatTemplate::getContent()
method to static does not work because it uses $this
which only makes sense in the context of an instance of a class. Therefore it triggers a fatal error in a static context.
You are using the AJAX-Chat library—you haven't stated what version you are using but there is an issue that discusses the error you encounterd.
In line with this reported issue, a commit to recent versions of this library were made to change this a static behaviour.
To resolve your issue, you have two choices:
Continue to use the version of AJAX-Chat that you have currently installed
Just use the AJAXChatFileSystem::getFileContents()
non-statically. Create an instance of the class and use that by modifying your getContent()
method like so:
function getContent()
{
if (!$this->_content) {
$ajaxChatFileSystem = new AJAXChatFileSystem();
$this->_content = $ajaxChatFileSystem->getFileContents($this->_templateFile);
}
return $this->_content;
}
Upgrade to the latest version of this library and use the static method
There does not appear to be a changelog, so you should test your code wherever you use AJAX-Chat to ensure there are no breaking changes.
Technically, you have a third choice: since this is an E_DEPRECATED
warning—implying that the functionality is flagged for removal at a future date—you can safely ignore this warning, for now.
E_DEPRECATED
warnings (as with all notices, warnings and errors) should be disabled from display to the user in production code.
However, I do not recommend this because your logs will be full of E_DEPRECATED
warnings. Moreover, as already mentioned, future versions of PHP may remove support for calling non-static methods statically.
Hope this helps :)