dongtaotao19830418 2013-06-21 14:42
浏览 20
已采纳

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

I am working on a CodeIgniter application.

A part of the nav menu for my application is generated from using session data. Since, I have to print the same thing at a number of places, I wrote a function to do the printing. The file which creates the menu is given below. The function print_roles_assigned() is used a number of times in this file.

$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;
}

The code given above didn't work. Out of any options, I resorted to using a $GLOBAL. An issue like this has never happened with me before and I am not sure if the use of $GLOBAL is appropriate. The new code is given below:

$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;
}

I would like to know:

  • Why my initial code failed to work?
  • Is the use of $GLOBAL appropriate?
  • What can be an alternate method to fix this issue?
  • 写回答

3条回答 默认 最新

  • dongxian2863 2013-06-21 14:51
    关注

    Why my initial code failed to work?

    Each function has what is called variable scope. Variables declared outside of the function are not accessible from within the function unless they are passed in as parameters, are members of a class that the function is a member of, or are explicitly declared to be global.

    There are three different ways to do this, take your pick.

    The easiest way to do this is to just pass the function in as a parameter 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;
    }
    

    Another option is to make $roles_assigned a member of the class, 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;
    }
    

    The other option (not recommended) is to use the global keyword 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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?