2013-05-19 44 views
1

我對PHP和CodeIgniter有一個奇怪的問題。這裏是我的文件的代碼:函數未初始化的第一個變量

編輯:跳過這段代碼,並轉到我寫的第二個。如果它可能有用,它仍然存在。

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

require_once ('utils.php'); 

class FooterHeader { 

    // Esta clase nos permite definir el footer y sus correspondientes menus 
    var $utils; 
    var $controller; 

    var $TOPMENU_NOT_LOGGED_IN_NAMES; 
    var $TOPMENU_NOT_LOGGED_IN_LINKS; 
    var $TOPMENU_LOGGED_IN_NAMES; 
    var $TOPMENU_LOGGED_IN_LINKS; 
    var $TOPMENU_DESIGNER_NAMES; 
    var $TOPMENU_DESIGNER_LINKS; 

    // Side menu 
    var $SIDEMENU_NOT_LOGGED_IN_NAMES; 
    var $SIDEMENU_NOT_LOGGED_IN_LINKS; 
    var $SIDEMENU_LOGGED_IN_NAMES; 
    var $SIDEMENU_LOGGED_IN_LINKS; 
    var $SIDEMENU_DESIGNER_NAMES; 
    var $SIDEMENU_DESIGNER_LINKS; 

    // Main menu 
    var $MAINMENU_NOT_LOGGED_IN_NAMES; 
    var $MAINMENU_NOT_LOGGED_IN_LINKS; 
    var $MAINMENU_LOGGED_IN_NAMES; 
    var $MAINMENU_LOGGED_IN_LINKS; 
    var $MAINMENU_DESIGNER_NAMES; 
    var $MAINMENU_DESIGNER_LINKS; 

    function FooterHeader ($controller) { 
    $this->utils = new Utils(); 
    $this->controller = $controller; 
    include_once 'defineMenus.php'; 
    configVars ($this); 
    } 

    public function setDefaultHeader ($situation) { 
    $topMenuNames = array(); 
    $topMenulinks = array(); 
    $MainMenuNames = array(); 
    $MainMenuLinks = array(); 
    $SideMenuLinks = array(); 
    $SideMenuNames = array(); 

    // dependiendo del estado del usuario, mostramos cosas u otras 
    if ($situation == NOT_LOGGED_IN) { 
     $topMenuNames = $this->TOPMENU_NOT_LOGGED_IN_NAMES; 
     $topMenuLinks = array ('users/login', 'users/register'); 
     $MainMenuNames = $this->MAINMENU_NOT_LOGGED_IN_NAMES; 
     $MainMenuLinks = $this->MAINMENU_NOT_LOGGED_IN_LINKS; 
     $SideMenuLinks = $this->SIDEMENU_NOT_LOGGED_IN_NAMES; 
     $SideMenuNames = $this->SIDEMENU_NOT_LOGGED_IN_LINKS; 
    } 
    else if ($situation == LOGED_IN) { 
     $topMenuNames = $this->TOPMENU_LOGGED_IN_NAMES; 
     $topMenuLinks = $this->TOPMENU_LOGGED_IN_LINKS; 
     $MainMenuNames = $this->MAINMENU_LOGGED_IN_NAMES; 
     $MainMenuLinks = $this->MAINMENU_LOGGED_IN_LINKS; 
     $SideMenuLinks = $this->SIDEMENU_LOGGED_IN_NAMES; 
     $SideMenuNames = $this->SIDEMENU_LOGGED_IN_LINKS; 
    } 
    else if ($situation == DESIGNER) { 
     $topMenuNames = $this->TOPMENU_DESIGNER_NAMES; 
     $topMenuLinks = $this->TOPMENU_DESIGNER_LINKS; 
     $MainMenuNames = $this->MAINMENU_DESIGNER_NAMES; 
     $MainMenuLinks = $this->MAINMENU_DESIGNER_LINKS; 
     $SideMenuLinks = $this->SIDEMENU_DESIGNER_NAMES; 
     $SideMenuNames = $this->SIDEMENU_DESIGNER_LINKS; 
    } 
    var_dump ($topMenuLinks); 
    $this->setHeader ($topMenulinks, $topMenuNames, $MainMenuLinks, $MainMenuNames, $SideMenuLinks, $SideMenuNames); 
    } 

    public function setHeader ($topMenuLinks, $linkNames, $menus, $menusNames, $sideMenuLinks, $sideMenuNames) { 
    var_dump ($topMenuLinks); 
    $enlaces = $this->generateTopMenu ($linkNames, $topMenuLinks); 
    $data['enlaces'] = $enlaces; 
    $menus = $this->generateMenus ($menusNames, $menus); 
    $data['menus'] = $menus; 
    $sideMenus = $this->generateSideMenu ($sideMenuLinks, $sideMenuNames); 
    $data['sideMenus'] = $sideMenus; 

    $this->controller->load->helper('html'); 
    $this->controller->load->view('static/header.php', $data); 
    } 
[...] 
} 

不要全部閱讀。這不是必需的。看看兩個var_dump的電話。第一var_dump打印出:

array (size=2) 
    0 => string 'users/login' (length=11) 
    1 => string 'users/register' (length=14) 

第二個:

array (size=0) 
    empty 

我不知道爲什麼我的變量未初始化。剩下的變量都可以。

編輯:我簡單的代碼到最大:

<?php 
class FooterHeader { 

    function FooterHeader ($controller) { 
    $this->controller = $controller; 
    } 

    public function setDefaultHeader ($situation) { 
    $topMenulinks = array(); 
    // dependiendo del estado del usuario, mostramos cosas u otras 
    $topMenuLinks = array ('users/login', 'users/register'); 
    var_dump ($topMenuLinks); 
    $this->setHeader ($topMenulinks); 
    } 

    public function setHeader ($topMenuLinks) { 
    var_dump ($topMenuLinks); 
    } 
} 

?> 

但問題仍然是存在的。

編輯:代碼做調用是這樣的:

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

require_once ('utils/footerHeader.php'); 
require_once ('utils/defineMenus.php'); 

class WelcomePage extends CI_Controller { 

    var $fh; 

    function WelcomePage() { 
    parent::__construct(); 
    $this->fh = new footerHeader($this); 

    $this->load->helper('url'); 
    } 
    public function index() 
    { 
    // seteamos el header (el defecto) 
    $this->fh->setDefaultHeader(NOT_LOGGED_IN); 

    $this->load->view('subirDisenyoEjemplo.php'); 

    //$this->fh->setFooter(); 
    } 
} 

/* End of file welcomePage.php */ 
/* Location: ./application/controllers/welcomePage.php */ 
?> 
+0

你也可以說是調用這些函數的代碼? – anomaaly

+0

我不確定你使用的PHP或CodeIgniter的版本是什麼,但要小心使用'var'來聲明你的變量,因爲這些變量都是'public'。 [用戶指南中的更多信息。](http://php.net/manual/en/language.oop5.visibility.php) – jleft

+0

你能解釋一下爲什麼你寫了「不讀全部,這是不必要的」,只是之後,當我讀了所有? – Voitcus

回答

1

編輯:當心資本:

var_dump ($topMenuLinks); 
$this->setHeader ($topMenulinks); 

$頂部菜單大號油墨與$頂部菜單油墨。我在我的服務器上嘗試了它,並立即彈出一個未初始化的變量。

原:

您的陣列的含量取決於if的結果 - else if結構。只有在這種情況下

if ($situation == NOT_LOGGED_IN) { 

將所述陣列被分配它的值如在第一var_dump()。否則,因爲它被當作

$topMenulinks = array(); 

,如果您有任何situation未涵蓋的LOGED_INDESIGNER初始化,你將最終獲得一個空數組。注意丟失GLOGED_IN

+0

謝謝謝謝謝謝!只是非常愚蠢。我現在要睡覺了...... =) – user1726613

+0

我的編輯器突出顯示了相同的變量,但它不關心大寫字母...... -_- – user1726613

+0

不客氣,晚安。看看Netbeans,它對我來說很好。另外,將服務器設置爲'E_STRICT',在它回來咬你之前得到錯誤:) – likeitlikeit

1

沒有調用代碼這是一種很難確定,但我沒有找到這樣的:

$situation == LOGED_IN 
$situation == LOGGED_IN 
+0

請參閱我的編輯。我儘量簡化了代碼,但仍然在發生。第二個var_dump返回一個空數組,但不是第一個:(( – user1726613

+0

檢查出:'$ topMenuLinks'和'$ topMenulinks'國會大廈L與小寫l。 – anomaaly