Re: PEAR_DelegateOwner

From: Date: Sat, 15 Nov 2003 21:53:33 +0000
Subject: Re: PEAR_DelegateOwner
References: 1 2  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-23663@lists.php.net to get a copy of this message
On 15 Nov 2003, at 9:17 AM, Martin Jansen wrote:
On Fri Nov 14, 2003 at 05:2645PM -0500, LingWitt@insightbb.com wrote:
I have added comments to the code in the proper format. I would much appreciate it if someone in authority could comment on this.
Looks cool for me. Do you want to make it part of the PEAR installer or should it be an independent package? (I'd favour the second one.)
This functionality is more of an improved PEAR_Autoloader, sans autoloading (as PHP 5 does that), so it would seem more appropriate to be included in the PEAR package itself. Moreover, this class seems too small to warrant an exclusive package, and its purpose has the potential to be the backbone of many a project, so it would do well to have it installed by default. This is of course my own opinion, and its up to you guys.
Also, how should errors be handled and where are they necessary?
Usually we do use PEAR::raiseError() / $this->raiseError() for that. Did I miss something?
For methods that don't exist, I'm calling trigger_error so that it is more in line with undefined methods treated by PHP. For the others, I will use the PEAR scheme. Also, here are the method processing methods with error detection. Since the error is fatal, can I include HTML in the output?: protected function cacheMethod($method)
        {
            foreach ($this->_delegates as $delegate)
            {
                if (method_exists($delegate, $method))
                {
                    $this->_method_map[$method] = $delegate;
                    return;
                }
            }
            $backtraceArray = debug_backtrace();
            $file   = "unknown file";
            $line   = "unknown line";
            foreach ($backtraceArray as $fileRecord)
            {
                if (strtolower($fileRecord["function"]) == $method)
                {
                    if (array_key_exists("file", $fileRecord))
                    {
                        $file = $fileRecord["file"];
                        $line = $fileRecord["line"];
                    }
                    $class = $fileRecord["class"];
                    break;
                }
            }
            throw new Exception("Call to undefined method $class::$method() in $file on line $line");
        }
        public function __call($method, $args)
        {
            $method = strtolower($method);
	    $args = array_merge(array($this), $args);
            if (!array_key_exists($method, $this->_method_map))
            {
                try {$this->cacheMethod($method);}
                    catch(Exception $exception)
                    {
                        $this->raiseError("Fatal: " . $exception->getMessage(), -1, PEAR_ERROR_DIE);
                    }
            }
            return call_user_func_array(array($this->_method_map[$method], $method), $args);
        }
Should the PHP 5 errors be used? Thanks.
You're talking about exceptions?
Yes.

« previous php.pear.dev (#23663) next »