Exchange variables between functions in PHP without using global variables

I have a class to interact with memcache server. I have different functions for inserting, deleting and retrieving data. Initially, each function called memcache_connect() , however this was optional, for example:

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

will create three memcache connections. I worked on this by creating a construct for the class:

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

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

This is good, however, if I call the class inside other classes, for example:

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

then he still makes two calls to memcache_connect when he only needs it.

I can do this with global variables, but I would prefer not to use them if I don't need it.

An example of implementing global tables:

 $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 be only one memcache_connect call.

Is there a way to do this or should I use global variables?

+5
oop php global
Feb 15 '09 at 11:33
source share
4 answers

I would encode another class using a singleton pattern to get a single memcache instance. 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 use -

 $mc = MemCache::getInstance(); memcache_get($mc, ...) ... 
+9
Feb 15 '09 at 11:41
source share

Go to the MC instance:

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

and etc.

+5
Feb 15 '09 at 11:36
source share

I think you are looking for static properties here.

 class mc { private static $instance; public static function getInstance() { if (self::$instance== null) { self::$instance= new self; } return self::$instance; } private function __construct() { $this->mem = memcache_connect(...); } } 

This implements a basic singleton pattern. Instead of building a call to the mc::getInstance() object. Check out the singletons .

+2
Feb 15 '09 at 11:45
source share

You must use dependency injection. The singleton pattern and static constructs are considered bad practice because they are essentially global (and for good reason - they reinforced you by using any class you create, unlike some others).

Here's something like what you have to do to have easy maintenance.

 class MemCache { protected $memcache; public function __construct(){ $this->memcache = memcache_connect(); } } class Client { protected $MemCache; public function __construct( MemCache $MemCache ){ $this->MemCache = $MemCache; } public function getMemCache(){ return $this->MemCache; } } $MemCache = new MemCache(); $Client = new Client($MemCache); $MemCache1 = $Client->getMemCache(); // $MemCache and $MemCache1 are the same object. // memcache_connect() has not been called more than once. 
+1
Aug 10 '13 at 5:14
source share



All Articles