Jump to content

PHP 5->8 Conversion


Recommended Posts

Hi all,  need some help converting some old code I ran years ago

This is the class I am calling:-

class template
{
public static $vars = array();

public static function show($tpl_path)
{
extract(self::$vars, EXTR_OVERWRITE);
ob_start();
include $tpl_path;
$cont = ob_get_contents();
ob_end_clean();
return $cont;
}

This is it being called

# new template instance
$basetemplate = new template();

$m1 = 'frontpage';
$m2 = 'index';

$content = template::show($m1::$m2());

Standard error i'm getting is Fatal error: Uncaught Error: Non-static method

 

Changing $content = $basetemplate->show($m1::$m2()); doesn't solve it.

 

Any thoughts or suggestions?  Many thanks all

Link to comment
Share on other sites

n PHP, to call a static method, you should use the class name directly followed by ::, like ClassName::method(). Non-static methods are called on an instance of the class using the arrow operator (->).

Since show() is a static method, you should call it directly from the class without creating an instance of the template class. So your call should look like this:

 

$content = template::show($m1::$m2());

However, your error suggests that $m1::$m2() is not returning a valid class name. Ensure that $m1 is a class name and $m2 is a static method of that class. If you intend to call a static method of a class stored in a variable, you can use call_user_func() or call_user_func_array() like this:

$content = call_user_func(array($m1, $m2));

Or directly:

$content = $m1::$m2();

Make sure that $m1 holds the name of a class and $m2 holds the name of a static method of that class.

Best Regard

Danish hafeez | QA Assistant

ICTInnovations

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.