Examples simplified here:
If I try this is a class with static methods:
public static function chkLog($role)
{
$userrole = $this->getRequest()->getSession()->read('userrole');
return $userrole;
/// more code
And even tried:
public static function chkLog($role)
{
$userrole = Session::read('userrole');
return $userrole;
/// more code
All I get is errors:
Error: [Error] Using $this when not in object context
I even have at the top of the class with the static methods:
<?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:
public function chkLog($role)
{
$userrole = $this->getRequest()->getSession()->read('userrole');
return $userrole;
/// more code
And
And call this method through a __callStatic method:
<?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?