开发者

How to remove multiple instances and just have one instance while multiple function calls in php?

开发者 https://www.devze.com 2022-12-24 05:22 出处:网络
public function getHelperInstance() { $user = new Helper(); $user->set($result[\'data\']); return $user;
public function getHelperInstance()
{
    $user = new Helper();
    $user->set($result['data']);
    return $user;
}

I am calling getHelper() class multiple times and if $user is not empty than am calling getHelperInstance(), now in my case getHelperInstance() always creates a new instance of Helper() class and so every time I call getHelperInstance() function am creating a new instance of Helper() so is there any way where can I can just create one instance of Helper() and use it multiple times instead of creating a new instance ever开发者_运维技巧ytime. Any suggestions !!!

public function getHelper()
{
    $user = array();
    if (!empty($user))
    {
        $user = $this->getHelperInstance();
    }
    return $user;
}


Here is what Erich Gamma, one of the Singleton pattern's inventors, has to say about it:

"I'm in favor of dropping Singleton. Its use is almost always a design smell"

So, instead of a Singleton, I suggest to use Dependency Injection.

Create the Helper instance before you create what is $this. Then set the helper instance to the $this instance from the outside, either through a setter method or through the constructor.

As an alternative, create a Helper broker that knows how to instantiate helpers by name and pass that to the $this instance:

class HelperBroker
{
    protected $helpers = array();

    public function getHelper($name)
    {
        // check if we have a helper of this name already
        if(!array_key_exists($name, $this->helpers)) {
            // create helper and store for later subsequent calls
            $this->helpers[$name] = new $name;
        }
        return $this->helpers[$name];
    }
}

This way you can lazy load helpers as needed and will never get a second instance, without having to use Singleton. Pass an instance of the broker to every class that needs to use helpers.

Example with a single helper

$helper     = new Helper;
$someClass  = new Something($helper);

and

class Something
{ 
    protected $helper;
    public function __construct($helper)
    { 
        $this->helper = $helper;
    }
    public function useHelper() 
    { 
        $return = $this->helper->doSomethingHelpful();
    }
}

Inside $something you can now store and access the helper instance directly. You don't need to instantiate anything. In fact, $something doesn't even have to bother about how a helper is instantiated, because we give $something everything it might need upfront.

Now, if you want to use more than one helper in $someClass, you'd use the same principle:

$helper1    = new Helper;
$helper2    = new OtherHelper;
$something  = new Something($helper1, $helper2);

This list will get rather long the more dependencies you insert upfront. We might not want to instantiate all helpers all the time as well. That's where the HelperBroker comes into play. Instead of passing every helper as a ready instance to the $something, we inject an object that knows how to create helpers and also keeps track of them.

$broker    = new HelperBroker;
$something = new Something($broker);

and

class Something
{ 
    protected $helperBroker;
    public function __construct($broker)
    { 
        $this->helperBroker = $broker; 
    }
    public function doSomethingHelpful()
    { 
        $return = $this->getHelper('foo')->doSomethingHelpful();
    }
    public function doSomethingElse()
    { 
        $return = $this->getHelper('bar')->doSomethingElse();
    }
}

Now $something can get the helpers it needs, when it needs them from the broker. In addition, any class that needs to access helpers does now no longer need to bother about how to create the helper, because this logic is encapsulated inside the broker.

$broker    = new HelperBroker;
$something = new Something($broker);
$other     = new Other($broker);

The broker also makes sure that you only have one helper instance, because when a helper was instantiated, it is stored inside the broker and returned on subsequent calls. This solves your initial problem, that you don't want to reinstance any helpers. It also doesn't force your helpers to know anything about how to manage themselves in the global state, like the Singleton does. Instead you helpers can concentrate on their responsibility: helping. That's clean, simple and reusable.


It sounds like you are interested in the singleton pattern. If you are using PHP5+, you should be able to take advantage of PHP's OOP stuff.


Here's an article on how to implement a singleton in php4. (But I would strongly suggest updating to php5 if that is an option at all)

class Singleton {
    function Singleton() {
        // Perform object initialization here.
    }

    function &getInstance() {
        static $instance = null;
        if (null === $instance) {
            $instance = new Singleton();
        }
        return $instance;
    }
}


PHP 4 Singleton Pattern

FYI, if you have any control over which PHP version you use you really should migrate to PHP 5.

0

精彩评论

暂无评论...
验证码 换一张
取 消