create a helper file in your app directory and use composer to autoload the file
How to call a class only once (global variable scope)
on clean project in php (without framework), i have class that i need available in all classes (for example log, db) , what is the best OOP way to do it available without global variable or call to new object all the time ?
https://stackoverflow.com/questions/28730071/have-only-one-instance-of-a-class
https://stackoverflow.com/questions/3695663/functions-that-can-be-called-only-once
Write a static helper.
In cakephp I wanted to use session in a static class method, but I had to do a small work around:
<?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));
}
}
So in my static method I could then use:
$usersession = session::read('loggin');
Static methods are usually fine for direct calls, a simple use statement:
use App\Helpers\SessionHelper as session;
And the static class with methods are available, none of that new and $this all over.
That's why Taylor has facades, for an expressive good looking syntax.
In cake you have:
$this->getRequest()->getSession()->write('psch', $psch);
I said gees, how many arrows do you need.
This:
session::read('loggin');
is just plain prettier. But I only needed that in my static methods, $this is not allowed.
Please or to participate in this conversation.