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?
oop php global
zi3guw Feb 15 '09 at 11:33 2009-02-15 11:33
source share