# (Solved) Use session in static method (cakephp 4)

**URL:** <https://discourse.cakephp.org/t/solved-use-session-in-static-method-cakephp-4/6011>\
**Category:** Uncategorized\
**Created:** [April 29, 2019, 4:54pm UTC](https://discourse.cakephp.org/t/solved-use-session-in-static-method-cakephp-4/6011 "2019-04-29T16:54:35Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimgwhit](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/jimgwhit/32/378_2.png) [@jimgwhit](https://discourse.cakephp.org/u/jimgwhit)\
**Post date:** [April 29, 2019, 4:54pm UTC](https://discourse.cakephp.org/t/solved-use-session-in-static-method-cakephp-4/6011/1 "2019-04-29T16:54:36Z")

</div>

Examples simplified here:  
If I try this is a class with static methods:

```auto
    public static function chkLog($role)
    {
        $userrole = $this->getRequest()->getSession()->read('userrole');
       return $userrole;
       /// more code

```

And even tried:

```auto
    public static function chkLog($role)
    {
        $userrole = Session::read('userrole');
       return $userrole;
       /// more code

```

All I get is errors:

```ruby
Error: [Error] Using $this when not in object context

```

I even have at the top of the class with the static methods:

```auto
<?php

namespace App\Helpers;

use App\Controller\AppController;
use Cake\Core\Configure;
use Cake\Http\Session;
use Cake\Event\Event;

class ChkAuth extends AppController

```

In case the AppController is needed for session.

### However

If I change the methods to instance methods:

```auto
    public function chkLog($role)
    {
        $userrole = $this->getRequest()->getSession()->read('userrole');
       return $userrole;
       /// more code

```

### And

And call this method through a \_\_callStatic method:

```auto
<?php

namespace App\Helpers;

use App\Helpers\ChkAuth;

class Auth
{
    public static function __callStatic($method, $params)
    {
        $instance = ChkAuth::class;
        $c = new $instance;
        return $c->$method(...array_values($params));
    }
}

```

Of course all works with no errors as expected…

Two questions:  
How do you use session inside a static method. And why did the above not work?

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [April 29, 2019, 10:55pm UTC](https://discourse.cakephp.org/t/solved-use-session-in-static-method-cakephp-4/6011/2 "2019-04-29T22:55:43Z")

</div>

Seems like you’re confused as to the nature of static functions in general? `$this` is a reference to the current object; when a function is static, there _is no_ current object. Hence the error you get. Is there some reason you want/need to make the `chkLog` function static? You don’t show how it’s ultimately being used, so hard to tell whether it’s a requirement.

Inside of a static function, you can only access static data plus the variables passed in. One possibility would be to pass in the session or request object; middleware generally gets the request passed to it, for example. Another would be to somehow initialize a static member in the class. Lastly, as a last-ditch option if no other way works, you can use `Router::getRequest()` from pretty much anywhere to get that request object, from which you can get the session.

---

<div class="post-metadata">

**Author:** ![jimgwhit](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/jimgwhit/32/378_2.png) [@jimgwhit](https://discourse.cakephp.org/u/jimgwhit)\
**Post date:** [April 30, 2019, 12:14am UTC](https://discourse.cakephp.org/t/solved-use-session-in-static-method-cakephp-4/6011/3 "2019-04-30T00:14:29Z")

</div>

> [@Zuluru](#):
>
> Seems like you’re confused as to the nature of static functions in general? `$this` is a reference to the current object; when a function is static, there _is no_ current object.

I know the difference between static and instance. static uses self, instance uses $this. I have written frameworks. My problem is I cannot figure out how to get session to work in a static method in cakephp.

### In laravel I can:

```auto
    public static function userRole($role = null)
    {
        $userrole = Auth::user()->role;
        $checkrole = explode(',', $userrole);
        if (in_array($role, $checkrole)) {
            return $role;
        }
        return false;
    }

```

Usage:

```auto
    public function scopegetPets($query, $petsearch = '')
    {
        $petsearch = $petsearch . "%";
        $query = Pet::where('petname', 'like', $petsearch);
        if (ChkAuth::userRole('admin') === false) {
            $userid = Auth::user()->id;
            $query->where('ownerid', '=', $userid);
        }
        $results = $query->orderBy('petname', 'asc')->paginate(5);
        return $results;
    }

```

Just that easy:

### However in cakephp I had to write this class to call:

```auto
<?php

namespace App\Helpers;

use App\Helpers\ChkAuth;

class Auth
{
    public static function __callStatic($method, $params)
    {
        $instance = ChkAuth::class;
        $c = new $instance;
        return $c->$method(...array_values($params));
    }
}

```

### In order to call statically this class

```auto
<?php

namespace App\Helpers;

use App\Controller\AppController;

class ChkAuth extends AppController
{

    public function userRole($role = null)
    {
        $userrole = $this->getRequest()->getSession()->read('userrole');
        $checkrole = explode(',', $userrole);
        if (in_array($role, $checkrole)) {
            return $role;
        }
        return false;

        /// Other methods
    }

```

### usage

```auto
    public function getPets($offset = "", $rowsperpage = "", $petsearch = "")
    {
        $pagingQuery = "LIMIT {$offset}, {$rowsperpage}";
        $petsearch = $petsearch . "%";
        if (Auth::chkRole('admin') === true) {
            return DB::select("SELECT * FROM " . PREFIX . "pets " . $pagingQuery);
        }
        $authid = Auth::authId();
        return DB::select("SELECT * FROM " . PREFIX . "pets WHERE ownerid = :authid " . $pagingQuery, ["authid" => $authid]);
    }

```

As my original post says, I tried also.

```auto
$userrole = Session::read('userrole');

```

There should be an easy way to use session within a static method.

And I do not have $this in a static method, I only tried:

```auto
$userrole = $this->getRequest()->getSession()->read('userrole');

```

Since it’s in the docs that way.

In a static method I use:

```auto
$ismatch = self::userRole($role, $userrole);

```

### Note

All works with the \_\_callStatic class. But still session should easily work inside a static method without special work a rounds.

I know the difference between self and this.

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [April 30, 2019, 1:00am UTC](https://discourse.cakephp.org/t/solved-use-session-in-static-method-cakephp-4/6011/4 "2019-04-30T01:00:49Z")

</div>

Apologies, but I’m not sure why anyone that’s written frameworks would even consider using `$this` in a static function. So it seemed like a bit of a novice question.

Did you try `Router::getRequest()->session()`, as I suggested?

---

<div class="post-metadata">

**Author:** ![jimgwhit](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/jimgwhit/32/378_2.png) [@jimgwhit](https://discourse.cakephp.org/u/jimgwhit)\
**Post date:** [April 30, 2019, 1:14am UTC](https://discourse.cakephp.org/t/solved-use-session-in-static-method-cakephp-4/6011/5 "2019-04-30T01:14:53Z")

</div>

I will give it a try as soon as I can.

I cannot figure out why cakephp uses $this to get session data anyway.

`Router` did not work. I tryed

```auto
$session = new \Cake\Http\Session();
$userrole = $session->read('userrole');
return $userrole;

```

That works, but don’t want that.

Funny the docs state that:

> `Session::` `read` ( _$key_ )

That’s right from the docs.

Solved:

```auto
$userrole = \Cake\Routing\Router::getRequest()->getSession()->read('userrole');

```

Funny if you go to [Sessions - 4.x](https://book.cakephp.org/4.0/en/development/sessions.html) and search  
for Routing or Router on that page, nothing.

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [April 30, 2019, 4:32am UTC](https://discourse.cakephp.org/t/solved-use-session-in-static-method-cakephp-4/6011/6 "2019-04-30T04:32:17Z")

</div>

`Session::read($key)` is from the section of the manual describing the API provided by the `Session` class, not shown anywhere as actual code to use. _Immediately_ after that, it gives `$session->read('Config.language');` as an example, which assumes that you have the session object.

The section on that same page titled [Accessing the Session Object](https://book.cakephp.org/3.0/en/development/sessions.html#accessing-the-session-object) shows `$this->getRequest()->getSession()->read()` as a “basic example”. So you can tell it’s accessed through the request object, and not hard from there to find how to get that.

---

<div class="post-metadata">

**Author:** ![jimgwhit](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/jimgwhit/32/378_2.png) [@jimgwhit](https://discourse.cakephp.org/u/jimgwhit)\
**Post date:** [April 30, 2019, 4:38am UTC](https://discourse.cakephp.org/t/solved-use-session-in-static-method-cakephp-4/6011/7 "2019-04-30T04:38:46Z")

</div>

> [@Zuluru](#):
>
> The section on that same page titled [Accessing the Session Object](https://book.cakephp.org/3.0/en/development/sessions.html#accessing-the-session-object) shows `$this->getRequest()->getSession()->read()` as a “basic example”. So you can tell it’s accessed through the request object, and not hard from there to find how to get that.

No mention of using router anywhere. It’s like they hid that part somewhere.

I’m experienced with frameworks, and I couldn’t find it, how is someone new to cake suppose to find Router::getRequest()-\>getSession()-\>read

Router to get session, really. Come on.

Why not Session-\> to actually get session. A word that matches a word.

Just me I guess.

## In laravel you can:

```auto
use Illuminate\Support\Facades\Session; // have use statement

```

And right in a static method:

```auto
$userrole = Session::get('thisrole');

```

Router to get session, ???

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [April 30, 2019, 4:41am UTC](https://discourse.cakephp.org/t/solved-use-session-in-static-method-cakephp-4/6011/8 "2019-04-30T04:41:53Z")

</div>

The router has nothing to do with the session. The session is related to the request, and the request can be accessed through the router (as the primary singleton in the system).

Anyone is free to update the documentation if they think it can be improved.

---

<div class="post-metadata">

**Author:** ![jimgwhit](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/jimgwhit/32/378_2.png) [@jimgwhit](https://discourse.cakephp.org/u/jimgwhit)\
**Post date:** [April 30, 2019, 4:55am UTC](https://discourse.cakephp.org/t/solved-use-session-in-static-method-cakephp-4/6011/9 "2019-04-30T04:55:38Z")

</div>

> [@Zuluru](#):
>
> The router has nothing to do with the session. The session is related to the request, and the request can be accessed through the router (as the primary singleton in the system).

Is that statement or that knowledge anywhere in any of the documentation if so will you please give a reference to it at least.

And I mean where the docs actually say use router to get session.

I just never heard of using router to get session. But it does work.

---

<div class="post-metadata">

**Author:** ![karmicdice](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/karmicdice/32/1298_2.png) [@karmicdice](https://discourse.cakephp.org/u/karmicdice)\
**Post date:** [May 2, 2019, 8:43pm UTC](https://discourse.cakephp.org/t/solved-use-session-in-static-method-cakephp-4/6011/10 "2019-05-02T20:43:18Z")

</div>

> Funny if you go to [https://book.cakephp.org/4.0/en/development/sessions.html](https://book.cakephp.org/4.0/en/development/sessions.html) and search  
> for Routing or Router on that page, nothing.

That was really potlite of you to point it, you do know 4 is in dev, right?

Now coming to your query…

`$session = $this->getRequest()->getSession()`  
is the right way to go and then

`$session->read()` or `$session->write()`;

Laravel Facade is like a Request “Handler” and thus `this`.

Why would you prefer singleton style? Just curious 🙂

For future reference… To make it easier for you… Facade and getRequest are siblings.

---

<div class="post-metadata">

**Author:** ![jimgwhit](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/jimgwhit/32/378_2.png) [@jimgwhit](https://discourse.cakephp.org/u/jimgwhit)\
**Post date:** [May 2, 2019, 9:09pm UTC](https://discourse.cakephp.org/t/solved-use-session-in-static-method-cakephp-4/6011/11 "2019-05-02T21:09:03Z")

</div>

> [@karmicdice](#):
>
> Why would you prefer singleton style? Just curious

```auto
$this->getRequest()->getSession()->write

```

3 arrows?

I came up with a better solution since then, I just use the session class direct via  
a SessionHelper I wrote:

```auto
<?php

namespace App\Helpers;

use Cake\Http\Session;

class SessionHelper
{
    public static function __callStatic($method, $params)
    {
        $instance = Session::class;
        $c = new $instance;
        return $c->$method(...array_values($params));
    }
}

```

Then to use, just place a use statement at top of class:

```auto
use App\Helpers\SessionHelper as session;

```

And usage:

```auto
$userrole = session::read('userrole');

```

Expanded example:

```auto
    public static function chkRole($role = null)
    {
        $userrole = session::read('userrole');
        $checkrole = explode(',', $userrole);
        if (in_array($role, $checkrole)) {
            return true;
        }
        return false;
    }

```

Same helper then of course works in an instance or static method.

To me the The Scope Resolution Operator as Taylor says is a

> terse, expressive syntax

---

<div class="post-metadata">

**Author:** ![karmicdice](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/karmicdice/32/1298_2.png) [@karmicdice](https://discourse.cakephp.org/u/karmicdice)\
**Post date:** [May 2, 2019, 9:15pm UTC](https://discourse.cakephp.org/t/solved-use-session-in-static-method-cakephp-4/6011/12 "2019-05-02T21:15:59Z")

</div>

You are internally calling the instance 😑

I do not see a performance gain. Yes, the code is cleaner I agree but using singleton just to avoid 3 arrows, not a reason I have heard.

`Cake\View\Helper\SessionHelper` is effecient.

I respect your opinion. Have it that way.

---

<div class="post-metadata">

**Author:** ![jimgwhit](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/jimgwhit/32/378_2.png) [@jimgwhit](https://discourse.cakephp.org/u/jimgwhit)\
**Post date:** [May 2, 2019, 9:22pm UTC](https://discourse.cakephp.org/t/solved-use-session-in-static-method-cakephp-4/6011/13 "2019-05-02T21:22:03Z")

</div>

> [@karmicdice](#):
>
> avoid 3 arrows

I wasn’t trying to avoid 3 arrows, I needed to use it in a static method is the main reason.

I like static methods in helpers.

I could have used

```auto
Router::getRequest()->getSession()->read('userrole');

```

But is that technique getting at times more than needed?

Or is my static call getting the same amount from the request? That part is where I’m unsure.
