call function from external class in add_menu_page
-
Hello everyone!! I need a little explanation if possible… let’s see this example:
file one.php <?php require_once( PLUGIN_DIR . 'two.php' ); require_once( PLUGIN_DIR . 'three.php' ); ?> file two.php <?php class static one { public static $three; public static function init() { $three = new init_three(); add_menu_page('...', '...', 'manage_options', 'two-plugin', array(&$three, 'init_three')); } } ?> file three.php <?php public static function init_three() { echo "<h1>Just a new dashboard!</h1>"; } ?>why using array(&$three, ‘init_three’) returns to me this error :
Warning: call_user_func_array() expects parameter 1 to be a valid callback, first array member is not a valid class name or object
indeed if I call directly ‘three::init_three’ it works?… I am asking because also on Stackoverflow I saw samples using array(<class variable>, ‘<class function’s>’)…..
Thanks a lot in advance!
Cheers
-
You’ve mixed up what sort of elements you’re using.
init_three()is not a class, it’s just a function. It can be referred to directly. There’s nothing to instantiate withnew. I’m not even sure what bearing thestatickeyword has outside of a class declaration. In a sense all procedural functions are “static”.For the sake of discussion, let’s say
init_three()was declared inside of classthree. Then you could do$three_obj = new three();. But$three_objis a class object. Also, static classes don’t need to be instantiated (but we wander off topic). What you need to add a menu page is the class name. Thus youroneclass would need to assigns a value to the$threeproperty so its value is available when you add a menu page.class static one { public static $three = 'three'; //etc.... }But then, to reference such a property, you still must include the class reference:
array( one::three, 'init_three')
But ideally you’d also define a property getter method and use that to fetch the value.If you want to just do
array( $three, 'three_init'), then you must assign to$threea value procedurally, outside of a class.
The topic ‘call function from external class in add_menu_page’ is closed to new replies.