Re: How expensive is glob'ing a dir and including all the files? [message #179150 is a reply to message #179149] |
Sat, 15 September 2012 22:06 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma:
|
Senior Member |
|
|
On 9/15/2012 5:41 PM, Anders Wegge Keller wrote:
> 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?
>
Not personally, but I've seen it happen to others.
>> 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?
>
Nope, but I'm not necessarily the only developer on a project, nor am I
necessarily the only one who will ever work on the project. In fact,
chances are I will not be. I don't work on 5 page hobby sites.
>> 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...
>
Which you don't...
>> Plus the extra overhead of searching for the module, of course.
>
> Surely, you are jesting. Or should I ask "Once unwitting, twice shy"?
>
Nope, it can take significant amount of time - I've seen some long paths
in the auto append.
>> 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.
>
Sure, which means the interpreter has to load and parse even more code
unnecessarily. And there is no such thing as an "already-loaded array" -
every invocation of a script starts fresh.
But then we all know when you only work on 5 page sites which receive
100 page views/day, you can get away with this. But we know you'd never
be able to handle a decent sized site.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|