CodeIgniter Forums
Object of type CodeIgniter\Session\Session is not callable - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Object of type CodeIgniter\Session\Session is not callable (/showthread.php?tid=87508)

Pages: 1 2


Object of type CodeIgniter\Session\Session is not callable - donpwinston - 04-26-2023

Can anyone suggest why this error would occur?

I'm referencing a view parameter like this:

Code:
Object <?php if (!isset($session) || !$session('logged_in')): ?>

The error is:

Object of type CodeIgniter\Session\Session is not callable

I recently upgraded to 4.3.3 but maybe I did something else also.


RE: Object of type CodeIgniter\Session\Session is not callable - kenjis - 04-26-2023

Yes, CodeIgniter\Session\Session is not callable.
What is the $session ?


RE: Object of type CodeIgniter\Session\Session is not callable - donpwinston - 04-27-2023

(04-26-2023, 03:48 PM)kenjis Wrote: Yes, CodeIgniter\Session\Session is not callable.
What is the $session ?

I passed it as a variable in a view function in a controller:

PHP Code:
print view('header', [
 
'current' => 'register',
 
'title' => 'BNC New User Agreement',
 
'meta_desc' => 'Register here for electronic bankruptcy noticing or to establish a preferred address!',
 
'session' => $this->session,
 ]); 

I fixed it by not doing that and used the session('logged_in') function in my view instead. Probably a better way to do it anyway. I have another application where I do the same thing and it does not give me this "session is not callable" error. I don't know what is different, both are using 4.3.3.


RE: Object of type CodeIgniter\Session\Session is not callable - kenjis - 04-27-2023

$this->session is a Session object. session() is the wrapper function for the shared Session object.
See https://codeigniter4.github.io/CodeIgniter4/libraries/sessions.html#retrieving-session-data


RE: Object of type CodeIgniter\Session\Session is not callable - donpwinston - 04-28-2023

(04-27-2023, 03:42 AM)kenjis Wrote: $this->session is a Session object. session() is the wrapper function for the shared Session object.
See https://codeigniter4.github.io/CodeIgniter4/libraries/sessions.html#retrieving-session-data

I know but why is referencing $this->session in a view file giving me the Session not callable error?


RE: Object of type CodeIgniter\Session\Session is not callable - kenjis - 04-28-2023

It seems you don't understand the difference between object and function clearly.

These are different:
PHP Code:
$session('logged_in') or $this->session('logged_in')
session('logged_in'



RE: Object of type CodeIgniter\Session\Session is not callable - donpwinston - 04-30-2023

I know the difference.

$this->session is not a function. It is the session object. I have a session variable in my controller. I'm using it as a param of the view() function. e.g print view('myview', ['session' => $this->session]); It is giving me an error 'Session is not callable'. I've been doing this for many years a zillion times. It now no longer works. Something is screwed up.


RE: Object of type CodeIgniter\Session\Session is not callable - donpwinston - 05-01-2023

(04-30-2023, 12:46 PM)donpwinston Wrote: I know the difference.

$this->session is not a function. It is the session object. I have a session variable in my controller. I'm using it as a param of the view() function. e.g print view('myview', ['session' => $this->session]); It is giving me an error 'Session is not callable'. I've been doing this for many years a zillion times. It now no longer works. Something is screwed up.

Maybe composer setup is hosed. Don't have the problem in another app on another server where I do the same thing.


RE: Object of type CodeIgniter\Session\Session is not callable - iRedds - 05-01-2023

Your PHP is defective.
The Session class has never been Invokable. This means that your code could not work in principle.


RE: Object of type CodeIgniter\Session\Session is not callable - donpwinston - 05-02-2023

(05-01-2023, 04:36 PM)iRedds Wrote: Your PHP is defective.
The Session class has never been Invokable. This means that your code could not work in principle.

???

print view('my view', ['session' => session()]);


myview.php
...
<p><?= $session->xyz ?></p>
...

So the proper way to access session data in a CI view is to use the session() function or fetch the session data into another variable and pass that to the view? I don't see why it matters. This has been working in CI4 for many years.