可变数据在函数内部表现奇怪

时间:2013-06-21 14:42:47

标签: php codeigniter function scope

我正在开发CodeIgniter应用程序。

我的应用程序导航菜单的一部分是使用会话数据生成的。因为,我必须在很多地方打印相同的东西,我写了一个功能来做打印。创建菜单的文件如下所示。函数print_roles_assigned()在此文件中多次使用。

$roles_assigned = $this->session->userdata('roles_assigned');
function print_roles_assigned() {
    $output = '';
    if ($roles_assigned)
    {
        foreach ($roles_assigned as $role) {
            $output .= '<li>' . anchor('main/home/'.$role->role_name, $role->rol
e_name) . '</li>';
        }
    }
    else
    {
        $output .= '<li>No roles have been assigned.</li>';
    }
    return $output;
}

上面给出的代码不起作用。在任何选项中,我都使用了$GLOBAL。我之前从未发生类似这样的问题,我不确定$GLOBAL的使用是否合适。新代码如下:

$GLOBALS['roles_assigned'] = $this->session->userdata('roles_assigned'); // Change made here
function print_roles_assigned() {
    $output = '';
    $roles_assigned = $GLOBALS['roles_assigned']; // Using the global variable inside function
    if ($roles_assigned)
    {
        foreach ($roles_assigned as $role) {
            $output .= '<li>' . anchor('main/home/'.$role->role_name, $role->rol
e_name) . '</li>';
        }
    }
    else
    {
        $output .= '<li>No roles have been assigned.</li>';
    }
    return $output;
}

我想知道:

  • 为什么我的初始代码无效?
  • 使用$GLOBAL是否合适?
  • 解决此问题的替代方法是什么?

3 个答案:

答案 0 :(得分:2)

初始代码失败,因为未注入$ roles_assigned。将函数参数化为“function print_roles_assigned($ roles_assigned){..}”以访问$ roles_assigned变量。

答案 1 :(得分:1)

这听起来像是一个范围问题。尝试使用$this->roles_assigned来引用该数组。

IMO,以这种方式使用GLOBAL并不是一种好习惯。

答案 2 :(得分:1)

  

为什么我的初始代码无效?

每个函数都有所谓的变量范围。在函数内声明的变量不能从函数内部访问,除非它们作为参数传入,是函数所属的类的成员,或者被明确声明为全局。

有三种不同的方法可以做到这一点,请选择。

最简单的方法是将函数作为参数传递给I.E。

$roles_assigned = $this->session->userdata('roles_assigned');
function print_roles_assigned($roles_assigned) {
    $output = '';
    if ($roles_assigned)
    {
        foreach ($roles_assigned as $role) {
            $output .= '<li>' . anchor('main/home/'.$role->role_name, $role->rol
e_name) . '</li>';
        }
    }
    else
    {
        $output .= '<li>No roles have been assigned.</li>';
    }
    return $output;
}

另一种选择是让$roles_assigned成为该班的成员,即I.E。

$this->roles_assigned = $this->session->userdata('roles_assigned');
function print_roles_assigned() {
    $output = '';
    if ($this->roles_assigned)
    {
        foreach ($this->roles_assigned as $role) {
            $output .= '<li>' . anchor('main/home/'.$role->role_name, $role->rol
e_name) . '</li>';
        }
    }
    else
    {
        $output .= '<li>No roles have been assigned.</li>';
    }
    return $output;
}

另一个选项(不推荐)是使用全局关键字I.E。

$roles_assigned = $this->session->userdata('roles_assigned');
function print_roles_assigned() {
    global $roles_assigned;
    $output = '';
    if ($roles_assigned)
    {
        foreach ($roles_assigned as $role) {
            $output .= '<li>' . anchor('main/home/'.$role->role_name, $role->rol
e_name) . '</li>';
        }
    }
    else
    {
        $output .= '<li>No roles have been assigned.</li>';
    }
    return $output;
}