php - is_null function not working properly in my if statement -
i have if statement detects if user logged in , belongs division. quite simple.
there permissions.php class, returns user object if logged in or null.
protected $user = null; ... public static function instance() { if(!self::$instance) { self::$instance = new permissions(); } return self::$instance; } ... public function get_user() { return $this->user; }
there user.php class, has functions return division in:
public function is_manager() { return $this->is_manager; }
so should able following:
if(permissions::instance()->get_user()->is_manager())
but of course might throw warning null objects, thought following:
if( ( ! is_null( permissions::instance()->get_user() ) ) && ( permissions::instance()->get_user()->is_manager() ))
which should check null , if not, should evaluate other half of if statement, php reason, evaluates of , still complains null object (when user not logged in).
so broke if statement nested if statement, , php still complained null object, led me believe is_null method wasn't working expected.
this worked in end:
if (permissions::instance()->get_user() != null) { if(permissions::instance()->get_user()->is_manager()){ ... } }
my question is, why did code is_null function not work?
upon further testing, of great comments, turns out get_user()
wasn't returning null, (bool)false
(from var_dump). why strange behaviour exhibited? class sets null @ top , function returns variable false?
because !=
not check type (null type) , get_user()
returned empty string or false (see php type comparison table).
check output of:
var_dump(permissions::instance()->get_user());
Comments
Post a Comment