“0 – Using $this when not in object context” Joomla Error After Updating to PHP 7.1+ – How to Handle

After updating PHP through EasyApache 4 to PHP 7.3 and then changing the PHP version in the MultiPHP Manager to 7.3, we encountered the following error on a Joomla website:

0 – Using $this when not in object context

A fast research revealed that this problem was caused by some legacy code in the template, and switching to a different template confirmed this. Debugging the page revealed the following:

Using $this when not in object context /home/[cpanel-user]/public_html/libraries/src/Application/CMSApplication.php:370
Call stack
# 	Function 	Location
1 	() 	JROOT/libraries/src/Application/CMSApplication.php:370
2 	Joomla\CMS\Application\CMSApplication::getMenu() 	JROOT/libraries/src/Application/SiteApplication.php:275
3 	Joomla\CMS\Application\SiteApplication::getMenu() 	JROOT/templates/[cpanel-user]/html/com_content/article/default.php:22
4 	include() 	JROOT/libraries/src/MVC/View/HtmlView.php:697
5 	Joomla\CMS\MVC\View\HtmlView->loadTemplate() 	JROOT/libraries/src/MVC/View/HtmlView.php:230
6 	Joomla\CMS\MVC\View\HtmlView->display() 	JROOT/components/com_content/views/article/view.html.php:210
7 	ContentViewArticle->display() 	JROOT/libraries/src/MVC/Controller/BaseController.php:672
8 	Joomla\CMS\MVC\Controller\BaseController->display() 	JROOT/components/com_content/controller.php:118
9 	ContentController->display() 	JROOT/libraries/src/MVC/Controller/BaseController.php:710
10 	Joomla\CMS\MVC\Controller\BaseController->execute() 	JROOT/components/com_content/content.php:43
11 	require_once() 	JROOT/libraries/src/Component/ComponentHelper.php:402
12 	Joomla\CMS\Component\ComponentHelper::executeComponent() 	JROOT/libraries/src/Component/ComponentHelper.php:377
13 	Joomla\CMS\Component\ComponentHelper::renderComponent() 	JROOT/libraries/src/Application/SiteApplication.php:194
14 	Joomla\CMS\Application\SiteApplication->dispatch() 	JROOT/libraries/src/Application/SiteApplication.php:233
15 	Joomla\CMS\Application\SiteApplication->doExecute() 	JROOT/libraries/src/Application/CMSApplication.php:196
16 	Joomla\CMS\Application\CMSApplication->execute() 	JROOT/index.php:49

A quick glimpse at the code revealed that the error wasn’t in the main index.php file of the template, so we renamed the html folder (which is located under templates/[joomla-template] folder) to html_old and the problem was resolved, which meant that the problem was in one of the folders under the html folder (e.g. the error was in one of the layout overrides). We renamed them one by one to folder_name_old, until we determined the culprit folder, which was com_content.

There was only one folder called article under the com_content folder and it only had one file, which was default.php. We opened that file and we changed this line:

$menu = &JSite::getMenu();

to:

$menu = JFactory::getApplication()->getMenu();

and that resolved the problem!

We hope that you found this very short post helpful. If you are having the same problem on on your Joomla website, then it is likely some old code in your Joomla template. If you can’t find/fix the old code in your Joomla template, then just contact us. We’ll address the problem for you swiftly, professionally, and affordably!

No comments yet.

Leave a comment