Re: Why change require_once? A brief explanation of motives

From: Date: Tue, 17 Jul 2007 10:53:43 +0000
Subject: Re: Why change require_once? A brief explanation of motives
Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-47545@lists.php.net to get a copy of this message
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. Pádraic Pádraic Brady http://blog.astrumfutura.com http://www.patternsforphp.com ----- Original Message ---- From: Lukas Kahwe Smith <mls@pooteeweet.org> To: Alexey Borzov <borz_off@cs.msu.su> Cc: Alan Knowles <alan@akbkhome.com>; Greg Beaver <greg@chiaraquartet.net>; PEAR developer mailinglist <pear-dev@lists.php.net> Sent: Tuesday, July 17, 2007 11:22:41 AM Subject: Re: [PEAR-DEV] Why change require_once? A brief explanation of motives Alexey Borzov wrote: > Hi, > > Lukas Kahwe Smith wrote: >> What are you arguing against? >> >> Is having to define that __autoload() function really worth not >> providing this flexibility? > > Failure to __autoload() a class always results in a fatal error, > exceptions thrown in __autoload() cannot be caught. You didn't provide > sample code Philippe asked you about [1] and I suppose that's because > you simply can't. ====== Bar.php ======= class Bar { public static function factory($driver) { $class_name = 'Bar_'.$driver; if (__autoload($class_name)) { return new $class_name(); } throw new Exception('unable to load'); } } ====== Bar/Meat.php ======= class Bar_Meat { } ====== foo.php ======= function __autoload($class_name) { // if the user has some kind of preference about how to confirm that the file // exists, or how syntax errors should be handled when __autoload() is called // directly, then he could implement those here return include str_replace('_', '/', $class_name).'.php'; } $bar = Bar::factory('Meat'); var_dump(get_class($bar)); $bar = Bar::factory('Meaty'); var_dump(get_class($bar)); ========== This gives me the output: Bar_Meat + 2 Warnings and an Exception Now the "ugly" part in all of this, is something we have been debating on this list before without a proper agreement. The first one is the fact that in order to prevent those warnings, you would have to suppress the include with an @ or you would need to attempt to find the file in the file system first, which is harder than one would hope for when relying on the include_path, since internals does not think we need an include_path search parameter in file_exists() like is available in fopen(). The work arounds to this are either not truely reliable or are inefficient and of course this is all open to race conditions. The other issue are potential syntax errors in the file to be included. The good news is that the user is now in control over how this should be dealt with inside the __autoload(). The bad news is that the user would need to implement all of this special handling for factory loaders in the generic implementation that gets call in all other cases as well, and there is no way to pass in any parameter to __autoload() to differentiate explicit loading attempts compared to implicit ones caused by making an instance of a not yet defined class. In the end I again prefer the added flexibility, since instead of all the back and forth in the past, where the PEAR developers were essentially the ones deciding what hack to do, its now in the application developers hands (though we can still provide him with reliable implementations for this choice). regards, Lukas -- PEAR Development Mailing List (http://pear.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php ____________________________________________________________________________________ Luggage? GPS? Comic books? Check out fitting gifts for grads at Yahoo! Search http://search.yahoo.com/search?fr=oni_on_mail&p=graduation+gifts&cs=bz

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