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

Posted by Rachel on Stack Overflow See other posts from Stack Overflow or by Rachel
Published on 2010-03-24T20:03:07Z Indexed on 2010/03/24 20:13 UTC
Read the original article Hit count: 232

Filed under:
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 everytime. Any suggestions !!!

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

© Stack Overflow or respective owner

Related posts about php