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

我不知道为什么我的变量未初始化。其余变量都OK。

简化代码:

<?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 */
?>
4

2 回答 2

1

注意大小写:

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

$topMenu L墨水与 $topMenu l墨水。我在我的服务器上尝试了它,它立即作为一个未初始化的变量弹出。

起初:

数组的内容取决于if-else if构造的结果。仅在这种情况下

if ($situation == NOT_LOGGED_IN) {

数组是否会像第一个一样被赋值var_dump()。否则,您将得到一个空数组,因为它被初始化为

$topMenulinks = array();

如果您有任何situation未涵盖的LOGED_INDESIGNER。注意中缺少 的G。LOGED_IN

于 2013-05-19T19:59:09.397 回答
1

如果没有调用代码,很难确定,但我确实发现了这一点:

$situation == LOGED_IN
$situation == LOGGED_IN
于 2013-05-19T20:01:33.657 回答