开发者

Strange behavior in a codeigniter library

开发者 https://www.devze.com 2023-03-26 08:13 出处:网络
I\'ve made an incredibly basic library, with a single function to check if the user is logged in now or not.

I've made an incredibly basic library, with a single function to check if the user is logged in now or not.

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

class Lib {

    public function is_logged_in()
    {
        $CI =& get_instance();
        if($CI->session->userdata('uid') === FALSE)
        {
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }
}

/* End of file Lib.php */

If I call the function from a controller, the code is definitely run and when not logged in it makes it into the if statement. I can add an echo to test that the code is definitely executed, but never is anything returned. If I change the return to a number, nothing is returned. If I change the return to a string, then and only then is stuff actually returned. I'm calling the function like $this->lib->is_logged_in(), and have added the library to the autoload.php file.

The 开发者_开发问答return is definitely executed as the function is exited. error reporting is set to E_ALL. Why in the heck won't this work?

(also yes I do realize the function isn't complete and secure yet.)


How are you determining that the return is executed? – NullUserException

@NullUserException: The code block exits. I can echo prior to either return and it will run, I can echo after either and it will not. – Cyclone

If you are trying to echo something after the return in your function, it will not work by design, I think this is probably universal to all languages. Anything after the return will not be executed. It will be parsed, but not executed.

var_dump($this->lib->is_logged_in()) should give you bool(true) or bool(false), or a "Trying to get property of non-object" error if lib is not loaded correctly.

Unless there is something you aren't sharing with us, the function should work as expected.

If you're still in doubt, assign the return value to a variable, then var_dump() the variable before you return it. Should be the same result.

EDIT: Sorry, I missed this in the long stream of comments:

var_dump() reveals that false is indeed being returned, why can't I simply echo it

I don't believe that echo'ing FALSE should give you any output, but echoing TRUE should give you 1.

This also has nothing to do specifically with Codeigniter.

For all practical purposes, there's no good reason to echo the return value of this function, or really anything where you expect a boolean return value. If it is returning the value you expect, then it is working.

0

精彩评论

暂无评论...
验证码 换一张
取 消