adzie Posted May 9 Share Posted May 9 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 Quote Link to comment Share on other sites More sharing options...
maxxd Posted May 10 Share Posted May 10 I take it there's a class called 'frontpage' with a non-static method called 'index'? Quote Link to comment Share on other sites More sharing options...
Danishhafeez Posted May 10 Share Posted May 10 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 Quote Link to comment Share on other sites More sharing options...
adzie Posted May 13 Author Share Posted May 13 Perfect thanks guys, that's been helpful! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.