CodeIgniter Forums
Unique instance of service - 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: Unique instance of service (/showthread.php?tid=79110)



Unique instance of service - FlorianL - 04-21-2021

Hey,

I want to make a single instance of a service to store class attributes between Controllers

Exemple :
In /ThirdParty i create class MyService
PHP Code:
<?php

namespace App\ThirdParty;

class 
MyService {

    protected $idRandom0;

    public function __construct() {
        $this->idRandom =random_int(010000);
    

    public function getIdRandom(){
        return $this->idRandom;
    }

    public function setIdRandom($code ''){
        $this->idRandom $code;
    }


In Config/Services :
PHP Code:
<?php

namespace Config;

use 
CodeIgniter\Config\BaseService;
use 
App\ThirdParty\MyService;

/**
 * Services Configuration file.
 *
 * Services are simply other classes/libraries that the system uses
 * to do its job. This is used by CodeIgniter to allow the core of the
 * framework to be swapped out easily without affecting the usage within
 * the rest of your application.
 *
 * This file holds any application-specific services, or service overrides
 * that you might need. An example has been included with the general
 * method format you should use for your service methods. For more examples,
 * see the core Services file at system/Config/Services.php.
 */
class Services extends BaseService
{
    
// public static function example($getShared = true)
    // {
    //     if ($getShared)
    //     {
    //         return static::getSharedInstance('example');
    //     }
    //
    //     return new \CodeIgniter\Example();
    // }

    
public static function myServ($getShared true){
    
    if ($getShared)
    
    {
        return static::
getSharedInstance('myServ');
    
    }
    
    return new MyService();
    }


In Controllers folder :
PHP Code:
<?php
namespace App\Controllers;

use 
App\Libraries\InstagramLibrary;

class 
ControllerOne extends BaseController
{

    public function __construct() {
        $this->myServ = \Config\Services::myServ();
    }

    public function index()
    {
        echo $myServ->getIdRandom();
    
And 
PHP Code:
<?php
namespace App\Controllers;

use 
App\Libraries\InstagramLibrary;

class 
ControllerTwo extends BaseController
{

    public function __construct() {
        $this->myServ = \Config\Services::myServ();
    }

    public function index()
    {
        echo $myServ->getIdRandom();
    

And when i go to the page the idRandom are not the same ... any explication ?


RE: Unique instance of service - paliz - 04-21-2021

random is rando what do you expect ? !!!!!!  can be same result


RE: Unique instance of service - FlorianL - 04-21-2021

(04-21-2021, 10:22 AM)paliz Wrote: random is rando what do you expect ? !!!!!!  can be same result

I want to have the same result into the 2 controllers :
On the first call of a controller : the service is init so the idRandom take a value between 0 and 10 000 for example 999
On a second call of the controller or another one which use this service i want to get the same instance to get the same idRandom in my example 999


RE: Unique instance of service - paliz - 04-21-2021

Okay first thanks u r using ci4
You should store init value in datebase or session for second ctl
This is not pussible call two ctl diffrent times then have same reult


RE: Unique instance of service - InsiteFX - 04-21-2021

use static variables.


RE: Unique instance of service - craig - 04-22-2021

If the controllers are called on separate requests, that is never going to work how you want it to.

'$this->idRandom =random_int(0, 10000);' is called for every request.

If you need to persist something between requests, use sessions or a database.


RE: Unique instance of service - FlorianL - 04-25-2021

(04-22-2021, 02:13 AM)craig Wrote: If the controllers are called on separate requests, that is never going to work how you want it to.

'$this->idRandom =random_int(0, 10000);' is called for every request.

If you need to persist something between requests, use sessions or a database.

Okey thanks a lot for your return ! I'll use database


RE: Unique instance of service - MGatner - 05-02-2021

Database would work fine but depending on the actual use of this scenario you may want to consider Session or Cache as more appropriate short-term persistence solutions.


RE: Unique instance of service - FlorianL - 05-02-2021

Thanks MGatner, session isn't the best solution because the goal is to have a unique instance of my service for all my clients.

The service's goal is to connect to a back-office. The connection is available for application not for users. So the session is specific for user