Re: include_once/__autoload/namespace emulation ...



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

>>XML
>>>Parser.inc.php
>>>Transformer.inc.php

>>DB
>>>Mysql.inc.php

>>FILE_SYSTEM
>>>File.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.

.



Relevant Pages

  • Re: PHP6 - Comments?
    ... >> For version 6, namespaces are becoming a topic again, since the sheer ... >> number of PHP projects will surely lead to name collisions. ... namespace collision is not a problem PHP ... > programmers' tendency to write rubbish documentation. ...
    (comp.lang.php)
  • Re: PHP6 - Comments?
    ... > For version 6, namespaces are becoming a topic again, since the sheer ... > number of PHP projects will surely lead to name collisions. ... required for that request, so how many times would you want to link to TWO ... Multiple inheritance does not exist in Java, so why should it exist in PHP? ...
    (comp.lang.php)
  • Re: [PHP] circular dependency between libraries
    ... problems and leave you with only the 'slightly' inelegant situation where you ... have to, internally, sync the 2 codebases now and again. ... If PHP had namespaces, I'd be happy. ...
    (php.general)
  • Re: [PHP] for the sake of conversation - syntax
    ... How does PHP work out where these "packages" are? ... The ability to have multiple namespaces per file leads to issues with the "as" or "into" syntax. ... private DbHandlerB $dbb = new DbHandlerB; ... One would hope that when namespaces are ubiquitous there will be no need to prefix functions with package identifiers. ...
    (php.general)
  • Re: Idea for PHP Enhancement: register_globals_manual
    ... And PHP 5 won't change the fact that there's no real namespaces in PHP. ... but that feature set is largely set in stone afaik. ... take my chances avoiding namespace collisions, ...
    (comp.lang.php)