Hi Dani,
My guess is that the constructor of your class is always overloaded. Will it be possible to write a new class that will responsible for checking if there is an instance of Memcached on each page?
You can use a simple singleton to act as the director or instantiator for all Memcached instance request. By doing this, you will be able to remove the object and others from the constructor and replace it with the singleton. So by doing this, the instance requests can be effeciently monitored.
If using the singleton, you will probably need to create a new method to handle all of your conditions that are located on your constructor.
Although Singleton's constructor is also loaded, but it is better to overload one class for monitoring purposes than overloading the constructor of the application class..
I really don't know if this can be of any help, but this is how I would do it on a much bigger application. Most importantly, if we have too many classes overlapping each other. Monitoring them is pretty tricky..
WARNING! singleton codes below are coming from top of my head... I getting ready for my flight to Princeton. I might have to double check on this, once I get into my computer..instead of a netbook. There might be some errors on the codes below..
<?php
final class GetInstance
{
private static $classIntance = array();
## in nature this contructor is overloaded, but only this one
private function __construct(){}
## we can then manage the instatiation of the classes as given by the variable your_class
public static function instantiate($your_class)
{
## we will only instantiate the $class as necessary
self::allowClass($your_class);
## we can then return the allowed or requested class above
return self::$classIntance[$your_class];
}
private static function allowClass($your_class)
{
## a simple check-point if the requested class even exists.
if (!array_key_exists($your_class , self::$classIntance))
{
self::$classIntance[$your_class] = new $your_class;
}
}
}
By implementing a simple singleton above, you new constructor will now look like this..
public function __construct(){
$this->cachedInstance = GetInstance::instantiate('Memcached');
}
Then the new method on the memched class can be something like this
public function newMethod('daniweb');
Then you add a new method on your posted class above to check all condtions.
public function checkConditions(){
$this->cache = $this->cachedInstance->newMethod('daniweb');
if ($this->cache->isPristine())
{
$this->cache->setOptions(array(
Memcached::OPT_NO_BLOCK => true,
Memcached::OPT_BUFFER_WRITES => true,
Memcached::OPT_BINARY_PROTOCOL => true,
Memcached::OPT_LIBKETAMA_COMPATIBLE => true,
Memcached::OPT_DISTRIBUTION => Memcached::DISTRIBUTION_CONSISTENT,
));
}
return true;
}
Do the same on the other... or they can co-exist in one method..
I am assuming here that Memcached::OPT_NO_BLOCK and the rest of it are static in the Memcached class or this could have been asigned as const... as in
const OPT_NO_BLOCK = array('true','false');
Some people may have disagreement with the singleton approach., this is because they have not tested it throughly, but this is a simple way of looking at the constructor overloading activity..
class A{
public function __construct(){
echo 'constructor of a or instance of a. always active and loaded';
}
public static function staticA(){
echo 'This is an static function and do not need the class A instantiated';
}
}
class B{
public function __construct(){
echo 'constructor of b always active and loaded when an instance of b is set';
}
}
## objects below will ouput the loaded constructors of those classes above.
$objectB = new B();
$objectA = new A();
## this does not need an instance and overloading of the constructor
A::staticA();
I really have to go...my flight was cancelled and I hope I get through this time... I already missed a week of school days ..Sorry for the long post.. I am not even sure if it will help or not..maybe for others.. :)