• Resolved IM_natascha

    (@im_natascha)


    Hello everyone!

    I created a Help-Class for Administration Menus, where I wanted to encapsulate all nessecary names and functionality required by wordpress to add one.

    The class structure looks like this:
    file AdminMenu.php

    abstract Class AdminMenu {
      // ... various variables
      // __construct() {}
      // ... various final methods
    
      abstract function register_with_wp();
    }
    
    Class ToplevelAdminMenu extends AdminMenu {
      // __construct() {}
      function register_with_wp() {
        add_menu_page (....);
      }
    }

    file Pluginname.php

    require_once(DIRECTORY.'AdminMenu.php'); // doesnot create fatal errors
    class Pluginname {
      __construct () {
        $this->adminmenu = new ToplevelAdminMenu(); // works fine
        $this->adminmenu->register_with_wp(); // breaks because of undefined function within this function called 'add_menu_page'
        add_menu_page(...); // PHP knows function
      }
    }

    In my Sub-Class I’ve got a problem: it says, I cannot access an undefined function. But: I create these objects within a plugin-file, so it has to be defined – or nor?

    Where can I access these functions like add_menu_page, cause, as it is described in the PHP-Docs, required files should have the same variables-visibility as the line where it was required.

    I’m still working on this but am thankful for any help provided.

    If it’s an PHP-specific problem, I am sorry for posting it in this forum, but I think that the wordpress communitiy is much more aware of the structure of wordpress than any other PHP-programmer.

Viewing 1 replies (of 1 total)
  • Thread Starter IM_natascha

    (@im_natascha)

    AdminMenu.php

    abstract Class AdminMenu {
      __construct($params) {}
      abstract function register_with_wp();
      final function add_Action() { add_action('admin_init', array(&$this, 'register_with_wp')); }
    }

    Benutzung:

    $menu = new AdminMenu($params);
    $menu->add_action();

    Die Funktion ist erst verfügbar, wenn do_action('admin_init') aufgerufen wird.

    Wers benutzen will: abstract Klassen kann man nicht direkt verwenden.

Viewing 1 replies (of 1 total)
  • The topic ‘add_menu_page not available from Class’ is closed to new replies.