isset() on codeigniter objects

20,000

Solution 1

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

Solution 2

You can check the existence of $this->session first

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

Share:
20,000
Patrick
Author by

Patrick

Updated on June 04, 2022

Comments

  • Patrick
    Patrick about 2 years

    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.