| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  You may not change or alter any portion of this comment or credits | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  of supporting developers from this source code or any supporting source code | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  which is considered copyrighted (c) material of the original comment or credit authors. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  This program is distributed in the hope that it will be useful, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  but WITHOUT ANY WARRANTY; without even the implied warranty of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @copyright       The XOOPS Project http://sourceforge.net/projects/xoops/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @license         http://www.fsf.org/copyleft/gpl.html GNU public license | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @author          trabis <[email protected]> | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 16 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  | class ProfileMainmenuPlugin implements MainmenuPluginInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |      * @return array | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     public function mainmenu() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $helper = \Xoops::getModuleHelper(basename(dirname(dirname(__DIR__)))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $subMenu = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         // Prevent wasting resources | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         if ($helper->isCurrentModule()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             $helper->loadLanguage('modinfo'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             // Edit Account | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             $subMenu[] = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |                 'name' => _PROFILE_MI_EDITACCOUNT, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |                 'link' => $helper->url('edituser.php'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             // Search Account | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             $subMenu[] = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                 'name' => _PROFILE_MI_PAGE_SEARCH, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                 'link' => $helper->url('search.php'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             // Change Password | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             $subMenu[] = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                 'name' => _PROFILE_MI_CHANGEPASS, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                 'link' => $helper->url('changepass.php'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $ret[] = [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             'name' => $helper->getModule()->getVar('name'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             'link' => $helper->url(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             'subMenu' => $subMenu, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         return $ret; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 55 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |  | 
            
                        
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.