Call to a member function X on a non-object

Chris D

XenForo developer
Staff member
Given this code:
PHP:
    public static function controllerPreDispatch(XenForo_Controller $controller, $action)
    {
        self::$controllerName = $controller->getRouteMatch()->getControllerName();
    }

Which when executed by the Tapatalk mobile app, produces this error:

Code:
ErrorException: Fatal Error: Call to a member function getControllerName() on a non-object - library/AVForums/Banners/Listener.php:288

Does the following seem like a reasonable solution?

PHP:
    public static function controllerPreDispatch(XenForo_Controller $controller, $action)
    {
        try
        {
            if (is_object($controller) && $controller instanceof XenForo_Controller)
            {
                self::$controllerName = $controller->getRouteMatch()->getControllerName();
            }
        }
        catch (Exception $e)
        {
            self::$controllerName = '';
        }
    }

Because, it doesn't work o_O

Regardless of what conditionals I wrap around this line:

PHP:
self::$controllerName = $controller->getRouteMatch()->getControllerName();

Tapatalk always throws this error:

Code:
ErrorException: Fatal Error: Call to a member function getControllerName() on a non-object - library/AVForums/Banners/Listener.php:288

Normal forum operation is fine. It's only a problem when we access the forum via the Tapatalk app.
 
PHP:
   public static function controllerPreDispatch(XenForo_Controller $controller, $action)
{
try
{
if (is_object($controller) && $controller instanceof XenForo_Controller)
{
$routeMatch = $controller->getRouteMatch();
if (is_object($routeMatch) && $routeMatch instanceof XenForo_RouteMatch)
{
self::$controllerName = $controller->getRouteMatch()->getControllerName();
}
}
}
catch (Exception $e)
{self::$controllerName = '';
}
}

What's that do? Might not be getting a route match.

[edit] Copying your code into the editor looks horrible, sorry.
 
Back
Top Bottom