ZF-722: Zend_Cache_Frontend_Function::call() Supporting object methods via callback parameter

Description

Currently the Zend_Cache_Frontend_Function::call() function does not support object methods. For two reasons I believe it should support object functions: 1) Use case for caching object method:


// part class
class part { 
     private $partnum;

     public function __construct($partnum) {
        $this->partnum = $partnum;
     }

     public function display() {
         // renders part and part information in elaborate html
         // relies on super expensive _getPartInfo() for data
         $info = $this->cache->call( array($this, '_getPartInfo') );
         echo "Part: {$this->partnum}";
         print_r($info);
     }

     private function _getPartInfo() {
         // query some databases
         // crunch some numbers
         // pretify data
         // ..
         return $info;
     }

     public function initCache() {
         // set up new Zend_Cache_Frontend_Function()
         // ..
     }
}

// main script
// parts list contains a list of part numbers
foreach ($partslist as $partnum) {
     $p = new Part($partnum);
     $p->display();
}

2) The Zend_Cache_Frontend_Function::call() documentation specifically states: {quote}Using the call() function is the same as using call_user_func_array() in PHP: {quote}

In order to accommodate this functionality the Zend_Cache_Frontend_Function::call() function's first parameter $name needs to allow for callbacks (http://us2.php.net/manual/en/…). My first implementation resulted in:


//Zend/Cache/Frontend/Function.php
    private function _makeId($name, $parameters)
    {
        /** callback handling fix **/
        if ( is_array($name) && is_object($name[0]) && is_string($name[1]) ) {
            $name = get_class($name[0])."::".$name[1];
        } else if ( is_array($name) ) {
            Zend_Cache::throwException('Incorrect callback format');
        }

        if (!is_string($name)) {
            Zend_Cache::throwException('Incorrect function name');
        }
        if (!is_array($parameters)) {
            Zend_Cache::throwException('parameters argument must be an array');
        }
        return md5($name . serialize($parameters));
    }

While this technically produced working functionality it had the draw back that the cached result was the same for every instance. I believe the cached result should not only be based on parameters passed to the function but also the variables in the scope of the function (which change generally per instance of an object). To achieve the feature I came up with another modification:


//Zend/Cache/Frontend/Function.php
    private function _makeId($name, $parameters)
    {
        /** callback handling fix **/
        $obj = null;
        if ( is_array($name) && is_object($name[0]) && is_string($name[1]) ) {
            $obj = $name[0];
            $name = get_class($name[0])."::".$name[1];
        } else if ( is_array($name) ) {
            Zend_Cache::throwException('Incorrect callback format');
        }

        if (!is_string($name)) {
            Zend_Cache::throwException('Incorrect function name');
        }
        if (!is_array($parameters)) {
            Zend_Cache::throwException('parameters argument must be an array');
        }
        if ($obj)
            return md5($name . serialize($obj) . serialize($parameters));
        else
            return md5($name . serialize($parameters));
    }

Thus concludes the new feature. Please forgive me if this does not belong here as it seemed to me the most practical place.

Comments

Fixed code tags

for the point 1, the Class frontend is not ok for you ?

no feedback