PHP function variable scope.



  • Ok, I'm either doing something idiotic.. or PHP is terrible.


    	/**
    * Add menu items to the database, called internally.
    *
    * @param array[] $menuitems Array of menu items.
    * @param int $top_menu_item_id ID of the upper menu_item.
    * @param int $module_id The modules ID.
    * @param int $menu_id The menus ID.
    * @return boolean true on success, false or error on problems.
    */
    private function addMenuItems($menuitems, $top_menu_item_id = null,
    $module_id = null, $menu_id = null) {
    $submenuitems = array(); //Array of menu items, fixes looping.
    $result = null; //Result of adding to this menu.
    $menuItemObj = null; //A menu object.
    $menu_item_id = null; //This menu item ID.

    foreach ($menuitems as $menuitem) {
    $menuItemObj = new MenuItem();
    $menuItemObj->setTopMenuItemID($top_menu_item_id);
    $menuItemObj->setMenuID($menu_id);
    $menuItemObj->setModuleID($module_id);
    $menuItemObj->setTitle(isset($menuitem["@"]["title"]) ?
    $menuitem["@"]["title"] : null);
    $menuItemObj->setLocation(isset($menuitem["location"]) ?
    $menuitem["location"] : null);
    $menuItemObj->setAccess(isset($menuitem["access"]) ?
    $menuitem["access"] : null);
    $menuItemObj->setEnabled(1);
    $result = $this->mediator->addMenuItem($menuItemObj);
    //The ID of the last added menu_item
    $menu_item_id = $this->mediator->getLastInsertID();

    if (isset($menuitem["menuitem"])) {
    if (!isset($menuitem["menuitem"][0])) {
    //What the? Why is this still populated? Crazy PHP.
    $submenuitems = array();
    $submenuitems[] = $menuitem["menuitem"];
    } else {
    $submenuitems = $menuitem["menuitem"];
    }
    $this->addMenuItems($submenuitems, $menu_item_id,
    $module_id, $menu_id);
    }
    }

    return true;
    }

    Hope that comes out right, havn't posted code here before.

    Basically the problem is the $submenuitems variable doesn't seem to be cleared on each run.
    This means if a single submenuitem menuitem comes through after a mass of submenu items it will insert all of the old menuitems as well as the new single menu item.
    The "What the? Why is this still populated?" is commenting my hack of a fix to clear the variable before adding to the array.

    This function is called further up by an addMenu function if that helps at all.

    Thanks :)



  • @danish said:

    Basically the problem is the $submenuitems variable doesn't seem to be cleared on each run.

    What do you mean, it doesn't get cleared on each run? You're initializing it to array(), right there at the beginning of the function. And it's local anyway, no way it could survive between function calls.

    @danish said:

    This means if a single submenuitem menuitem comes through after a mass of submenu items it will insert all of the old menuitems as well as the new single menu item.

    I'm not sure I understand. You mean, on successive calls of the function or on successive loops through the foreach?




  • Annndd I'm an idiot. After looking at it with a clear brain the problem was between the screen and the chair.



    Basically I managed to forget how I was calling the function. My theory
    was I was calling the function per menu item, when really I was passing
    in an array of menuitems then adding submenu items per menu item (deep
    breath
    ).



    Sorry for the hassle, and thanks for making my brain click :)


Log in to reply