Re: include_once/__autoload/namespace emulation ...
- From: Kevin Newman <CaptainN@xxxxxxxxxxx>
- Date: Tue, 29 Nov 2005 15:33:06 GMT
Sean wrote:
Namespaces and PHP are certainly an issue, some of the things you wrote I have never heard of before, like require_once being bugged.
Pear is a good example of how namespaces should be done but I would suggest an even simpler solution.
Say you have a folder structure like:
classes
XMLParser.inc.php Transformer.inc.php
DBMysql.inc.php
FILE_SYSTEMFile.inc.php
They you should have an auto load function like:
function __autoload( $class ) { $load = strtolower( str_replace( "_", "/classes/", $class ) ) . ".inc.php"; if( file_exists( $load ) ) { include_once( $load ); } else { die( "Can't find a file for class: $class \n" ); } }
Now you can have
$obj = new XML_Parser();
(example adapter from http://www.wiki.cc/php/Overload_autoload).
This makes things allot easier. The drawback is it is not php4 compatible.
I thought of that, but doesn't it try to load "XML_Parser" from within the class file, instead of just "Parser"? In some cases that would lead to to huge class names (and a lot of typing).
Also, I've read that there is a performance problem with __autoload - has that been taken care of? (I read that php 5.1 improved some of the performance issues with magic functions.)
I really wish the php guys would just implement at least class level namespaces (they already have a patch).
Kevin N. .
- Follow-Ups:
- References:
- include_once/__autoload/namespace emulation ...
- From: Kevin Newman
- Re: include_once/__autoload/namespace emulation ...
- From: Sean
- include_once/__autoload/namespace emulation ...
- Prev by Date: Re: Parameters to image.php
- Next by Date: Re: Parameters to image.php
- Previous by thread: Re: include_once/__autoload/namespace emulation ...
- Next by thread: Re: include_once/__autoload/namespace emulation ...
- Index(es):
Relevant Pages
|