简体   繁体   中英

Variable data behaving strangely inside a function

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?

The initial code failed because your $roles_assigned in not injected. Parameterise the function to "function print_roles_assigned($roles_assigned) { .. } " to access the $roles_assigned variable.

This sounds like a scope issue. Try using $this->roles_assigned for your reference to that array.

IMO, it's not good-practise to use GLOBAL in this way.

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 IE

$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, IE

$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 IE

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM