Laravel-Blade icon indicating copy to clipboard operation
Laravel-Blade copied to clipboard

I would like to get a Singleton instance always.

Open shafihuzaib opened this issue 9 years ago • 1 comments

I want to get a singleton instance, that is; I instantiate the Blade Class once (automatically) and then use a static function like Blade::instance() to get the object instantiated already. In this way, I won't have to worry about new Objects being made everytime.

F3 uses the following function to get the desired effect. Can I somehow manage to do the same here?

static function instance() { if (!Registry::exists($class=get_called_class())) { $ref=new Reflectionclass($class); $args=func_get_args(); Registry::set($class, $args?$ref->newinstanceargs($args):new $class); } return Registry::get($class); }

shafihuzaib avatar Jan 27 '16 21:01 shafihuzaib

Why don't you just use a container and inject it, if you don't want to "worry about new objects being made every time"?

chrisharrison avatar Jul 20 '17 11:07 chrisharrison