List Info

Thread: Re: Why change require_once? A brief explanation of motives




Re: Why change require_once? A brief explanation of motives
user name
2007-07-17 06:57:12
Still needs some tweaking; if I'm reading it right
PEAR2::callAutoload() would just call __autoload() if it
exists, and skip SPL. It might be worth exploring a standard
practice of using the SPL and then falling back on
spl_autoload_call(), or grabbing a list of such autoload
functions using spl_autoload_functions(). The second has the
benefit of returning FALSE if nothing is registered actually
- could be a useful detection check for SPL usage by the
user - and only then attempt __autoload() directly.

Kind regards,
Paddy
 
Pádraic Brady
http://blog.astrumfutura
.com
http://www.patternsforp
hp.com


----- Original Message ----
From: Lukas Kahwe Smith <mlspooteeweet.org>
To: Pádraic Brady <padraic.bradyyahoo.com>
Cc: PEAR Dev <pear-devlists.php.net>
Sent: Tuesday, July 17, 2007 12:19:14 PM
Subject: Re: [PEAR-DEV] Why change require_once?  A brief
explanation of motives

Pádraic Brady wrote:
> Just to note we should not directly refer to
"__autoload()" - many of us have applications
already tying this function declaration up. We could offer
an implementation (this was suggested earlier I think)
capable of inclusion using the SPL functions which isn't
going to conflict with existing __autoload() definitions.

foo.php would be user code.

Aside from that the fact that I am calling __autoload()
directly is an 
oversimplification. We obviously need to support both
__autoload() as 
well as the spl variant. I also just noticed that I forgot
to wrap 
things inside a class_exists() call in the factory.

Here is updated code, the spl detection and calling needs a
bit more 
tweaking probably, since I have not really used them so far.

===== PEAR2.php =====

class PEAR2
{
   public static function callAutoload($class_name)
   {
     static $function = null;
     if (is_null($function)) {
       if (function_exists('__autoload')) {
         $function = '__autoload';
       } else if (spl_autoload_functions()) {
         $function = 'spl_autoload_call';
       } else {
         throw new Exception('no autoload defined');
       }
     }
     return $function($class_name);
   }
}

===== Bar.php =====

class Bar
{
   public static function factory($driver)
   {
     $class_name = 'Bar_'.$driver;
     if (class_exists($class_name, false) || 
PEAR2::callAutoload($class_name)) {
       return new $class_name;
     }
     throw new Exception('unable to load');
   }
}

regards,
Lukas








 
____________________________________________________________
________________________
Need Mail bonding?
Go to the Yahoo! Mail Q&A for great tips from Yahoo!
Answers users.
http://answers.yahoo.com/dir/?link=list&sid=3965460
91
[1]

about | contact  Other archives ( Real Estate discussion Medical topics )