using PHP5 classes as poor man's function namespaces?

liunx

Guest
I maintain a project that typically includes ~65 files per script execution. Most of them are function libraries and the functions have been separated into different files along their functionality.

I know that include(), include_once(), require(), require_once() are some of the most expensive calls to make due to having to read in the files, parse them, etc.

What I was wondering about is using PHP5 classes as poor man's namespaces and using __autoload() to require_once() them as needed.

So in a file named class.Foo.inc.php I would have:

class Foo {
static public function helloWorld() {
echo 'Hello World!!';
}
}


And in another script:

function __autoload($class_name) {
require_once './class.'.$class_name.'.inc.php';
}

Foo::helloWorld();


That way only the files that are actually used in that script instance are included, instead of the glut of my function libraries just-to-be-safe.

Are there any downsides to this?
Heavier memory usage?
__autoload() affecting performance?
Caching problems? (like with eAccelerator)

Any suggestions and information is greatly appreciated!!I haven't tried this exact idea myself, though I don't see why it should have the expected effect (don't know what effect it may have caches or accelerators); but I have done something somewhat similar in PHP 4 on a project that involved the occasional use of large and expensive functions on an "if and only if necessary" basis, which I documented in this post (<!-- m --><a class="postlink" href="http://www.phpbuilder.com/board/showthread.php?s=&postid=10474602#post10474602">http://www.phpbuilder.com/board/showthr ... st10474602</a><!-- m -->). Notice that the example there works function by function.Thanks for the response.

I asked the same question on a couple other programming-related messageboards and the only response I got was, "HAHAH!! YOU'RE NOT A REAL PROGRAMMER BECAUSE YOU USE PHP!!"

I'm about 95% done with wrapping my functions with abstract classes and I hope to regex the source code to replace all nstances of the old function calls with the new <Faux_Namespace>::<Function_Name> syntax sometime later today.

With classes I'll have to be more careful to avoid namespace collisions with other classes because I use common names such as "Session", "File", "Image", etc. ... I may tweak the names to denote their custom nature, but I'd like to avoid having to type stuff like My_File::upload() (File::upload() seems cleaner)

I'll post my findings after I get it up and running so that anybody that is interested may get some real world benchmarks. By "benchmark" I mean I just lazily compare the difference in # of file includes, total filesize of includes, and script execution timeWell, the deed is done and I have to say that I am a little dissappointed. I didn't think that it would be a silver bullet, but my timer is reporting, on average, an additional 0.05 seconds on average.

Granted, the timer is instantiated when the template/page class is instantiated, so it is not 100% accurate. As you said, the old way (including all my libs at the top) probably was where the load latency was and now it is spread out over the script's execution. That, and there is most-likely a miss penalty when it fails to instantiate and goes to __autoload() for the second chance

It is now including anywhere from 1-12 less scripts that it used to, depending on the page. On average, the total size of the included files has gone down by around 100k.

Perhaps if I split up my functions into more classes then the file includes will be smaller yet, but then I run the risk of sometimes including even more files since i have one class per file.

In summary, it is basically a wash. Not really worth the effort if you have an established codebase. Then again, it'll be nice not having to worry about what to include and what not to include explicitly.
 
Back
Top