• Resolved Luigino

    (@luigino)


    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

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    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 with new. I’m not even sure what bearing the static keyword 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 class three. Then you could do $three_obj = new three();. But $three_obj is 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 your one class would need to assigns a value to the $three property 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 $three a value procedurally, outside of a class.

    Thread Starter Luigino

    (@luigino)

    Hello @bcworkz , thank you for your answer!

    • This reply was modified 3 years ago by Luigino.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘call function from external class in add_menu_page’ is closed to new replies.