Re: How expensive is glob'ing a dir and including all the files? [message #179149 is a reply to message #179148] |
Sat, 15 September 2012 21:41 |
Anders Wegge Keller
Messages: 30 Registered: May 2012
Karma:
|
Member |
|
|
Jerry Stuckle <jstucklex(at)attglobal(dot)net> writes:
> On 9/15/2012 2:08 AM, Anders Wegge Keller wrote:
>> "J. Frank Parnell" <juglesh(at)gmail(dot)com> writes:
>>
>>> So, my question is, am I saving a significant amount of ram/cpu/time
>>> by keeping this cache of filenames or should I just glob the dir and
>>> include what's in there everytime?
>>
>> You may want to consider autoloading classes as needed. See
>> http://php.net/manual/en/language.oop5.autoload.php
>>
>> This approach is of course dependent on using PHP5 *and* having an
>> OO design.
>>
>
> Additionally, you're never sure which module you got.
Speaking from experience?
> All kinds of hard-to-diagnose things can happen when someone uploads
> a module of the same name but earlier in the search path.
You let "someone" muck around with your code? Are you hosting at
GoDaddy or something like that?
> You'll be trying to debug one module while the system is using an
> entirely different module.
Only if you haven't got a clue...
> Plus the extra overhead of searching for the module, of course.
Surely, you are jesting. Or should I ask "Once unwitting, twice shy"?
> This is not a feature I recommend. It can cause many problems, and
> good programming techniques render it pretty much unnecessary.
I don't expect you to recognize a metric ton of clue, landing on your
head, but for the record let me tell the rest of the audience what the
self-proclaimed king of c.l.p is not smart enough to fiure out.
Wait for it...
Wait a bit more ...
The autoloader can be overridden by whatever code you care to call
when there is a request for an unknown class <rimshot />
[Simplified code ahead]
{classloader.php}
global $clClassMap;
$clClassMap = array(
/* Interfaces */
'iArticle' => 'classes/articleinterface.php',
/* Base classes */
'Page' => 'classes/pageobj.php',
'Request' => 'classes/request.php',
... <<The rest of whatever classes defined>>
);
class AutoLoader {
static function autoload ($classname) {
global $clClassMap;
$filename = false;
if ( isset ( $clClassMap[$classname] ) ) {
$filename=$clClassMap[$classname]
require("${ABSROOT}/${filename}");
} else {
return false; /* And throw an error. */
}
}
if ( function_exists ( 'spl_autoload_register' ) ) {
spl_autoload_register ( array('AutoLoader', 'autoload') );
} else {
function __autoload( $class ) {
AutoLoader::autoload( $class );
}
ini_set( 'unserialize_callback_func', '__autoload' );
}
{END classloader.php}
Everywhere else, require classloader.php, and use whatever classe you
want to. In case you forget to add them to $clClassMap, even Jerry
Stuckle should be smart enough to get what the error means. At least
after a few false starts. Also note that there is no noticable
overhead looking up an entry in a already-loaded array, compared to
the amortized overhead of the Stuckle-approved method of including and
parsing everything that has a remote chance of being relevant.
There might be a typo or two in the preceeding code. After all, it's
a simplified example. But I expect the majority of this group to be
able to work out the syntax errors. If not, please ask for
clarification.
--
/Wegge
Leder efter redundant peering af dk.*,linux.debian.*
|
|
|