isset() on codeigniter objects

Patrick picture Patrick · Apr 13, 2013 · Viewed 19.8k times · Source

How can I achieve something that looks like:

if (isset($this->session->flashdata('user_profile'))) {}

without:

Fatal error: Call to a member function flashdata() on a non-object in ...

The issue is that it returns an error when that thing isn't set rather than continue as one might expect. If it is set, everything works out fine.

...

I have also tried:

$tmp = $this->session->flashdata('user_profile');
if ($tmp) {

but to no avail.

Answer

itsme picture itsme · Apr 13, 2013

you don't need isset(); cause CI methods returns false or true

just do

 if($this->session->flashdata()){ }

and your error is cause you are surely not loading the session library:

so do this:

   $this->load->library('session');
if($this->session->flashdata()){ }

if you prefer (i preferr) you can autoload the session library changing this in your config/autoload.php file:

$autoload['libraries'] = array('session');

so now you don't have to load anytime the session library cause CI autoloads that for you