php - Unserialized object can't be passed from origin? -


so have complex object wish cache after creation expensive initialize. i'm able reconstitute instance within class defining file need able return reconstituted instance in place of new myclass if scheme going of use. (don't i?)

here's i've done far:

class payperiodservice {     public $type;           // weekly,bi-weekly, semi-monthly, monthlly     public $payday_first;     public $close_first;     public $hours_start;     public $hours_end;     public $length;         // in days     public $periods;        // array of periods year     public $dayinterval;     public $oneweekinterval;     public $twoweekinterval;     public $semifirstinterval;     public $monthinterval;     public $initialyear;     public $today;          // date object      public function __construct()     {         if( redis::exists('pay-period-instance')) {             log:info( 'fetching pay-period cache.');             $instance = json_decode(redis::get('pay-period-instance')); //            var_dump( $instance ); //            exit();             return $instance;         }         return $this->init();     }       public function init()     {         log::info('reconstituting pay-period primitive definition.');          $ppdef = payperiod::all()->last();         // etc etc etc, setting properties, loading arrays etc          // cache object          redis::set('pay-period-instance', json_encode($this));         return $this;     } } 

so when instantiate class, $ppsvc = new payperiodservice; in class, $instance variable in payperiodservice file valid , reconsituted, functional. returned instance in $ppsvc mindless zombie shell of ought be: no instance data, no methods.

what magic need invoke restored object travel abroad needs must do? have explored serializable interface, , tried un/serialize in place of json_encode/decode no significant change problem.

the problem __construct() method not return anything. want singleton (afaicu).

look @ example:

class {}  class b {   public function __construct(){return new a;} }  $b = new b;  print_r($b); // b 

so has see having constructor returning different class, not going happen. there several ways accomplish this, can take on web.

a simple example:

class payperiodservice {    /**    * @var self    */   static private $instance;    // private removes possibility make new instance   private function __construct()   {     // object construction logic here   }    /**    * @return payperiodservice    */   static public function getinstance()   {     if(!self::$instance)     {       self::$instance = new static;     }      return self::$instance;   } }  $ppsv = payperiodservice::getinstance(); // return intend 

unless object mutating on redis, trick. can adapt if needed


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -