Php – Share variables between functions in PHP without using globals

globaloopphp

I have a class for interacting with a memcache server. I have different functions for inserting, deleting and retrieving data. Originally each function made a call to memcache_connect(), however that was unnecessary, e.g.:

mc->insert()  
mc->get()  
mc->delete() 

would make three memcache connections. I worked around this by creating a construct for the class:

function __construct() {
    $this->mem = memcache_connect( ... );
}

and then using $this->mem wherever the resource was needed, so each of the three functions use the same memcache_connect resource.

This is alright, however if I call the class inside other classes, e.g.:

class abc
{
    function __construct() {
        $this->mc = new cache_class;
    }
}    
class def
{
    function __construct() {
        $this->mc = new cache_class;
    }
}

then it is still making two memcache_connect calls, when it only needs one.

I can do this with globals but I would prefer not to use them if I don't have to.

Example globals implementation:

$resource = memcache_connect( ... );

class cache_class
{
    function insert() {
        global $resource;
        memcache_set( $resource , ... );
    }
    function get() {
        global $resource;
        return memcache_get( $resource , ... );
    }

}

Then no matter how many times the class is called there will only be one call to memcache_connect.

Is there a way to do this or should I just use globals?

Best Solution

I would code another class using singleton pattern for getting the only instance of memcache. Like this -

class MemCache 
{ 
  private static $instance = false;   
  private function __construct() {}

  public static function getInstance()
  { 
    if(self::$instance === false)
    { 
      self::$instance = memcache_connect(); 
    } 

    return self::$instance; 
  } 
}

and usage -

$mc = MemCache::getInstance();
memcache_get($mc, ...)
...