MyBB Community Forums

Full Version: PHP exception when using add_shutdown() function with static method
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,

if I use the add_shutdown() function (located in inc/functions.php) with a class name and a static method (both given as strings) the add_shutdown()-function raises an exception because it uses the PHP get_class()-function inside which allows only objects as first parameter since PHP version 5.3.x (I'm using 5.3.5).

class foo
{
	public function someMethod()
	{
	
	}
	
	public static function someStaticMethod()
	{
	
	}
}

$fooInstance = new foo();

// Works fine
add_shutdown($fooInstance, 'someMethod');

// Works fine with PHP 5.2.x but not with PHP 5.3.x
add_shutdown('foo', 'someStaticMethod');

A possible solution would be to add an is_string()-call in line 121 of "inc/functions.php" (MyBB version 1.6.5) in add_shutdown()-method:

$shutdown_functions["class_".(is_string($name[0]) ? $name[0] : get_class($name[0]))."_".$name[1]] = array('function' => $name, 'arguments' => $arguments);

Thanks in advance for some feedback,
Stephan Schulze (Germany)
Is there a solution yet?

Thanks for any reply,
Stephan
You could add a function outside a class to call your static function. It's at least easy. Wink
Yes - that would be the easiest way.
But if you have a bigger plugin with some classes it's a little bit ... dirty ... to put some of the code in an external, "stand-alone" function.

And as I patched the core files by myself I can't see any problems with the suggested patch.

Cheers,
Stephan
Hi everybody,

is there any update on this topic?

Cheers,
Stephan
Add your own function.
function foo_add_shutdown($name, $arguments=array())
{
	global $shutdown_functions;
	
	if(!is_array($arguments))
	{
		$arguments = array($arguments);
	}

	if(is_array($name) && method_exists($name[0], $name[1]))
	{
		$shutdown_functions["class_".(is_string($name[0]) ? $name[0] : get_class($name[0]))."_".$name[1]] = array('function' => $name, 'arguments' => $arguments); 
		return true;
	}
	else if(!is_array($name) && function_exists($name))
	{
		$shutdown_functions[$name] = array('function' => $name, 'arguments' => $arguments);
		return true;
	}

	return false;
}

Easiest way.