Re: include_once/__autoload/namespace emulation ...



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

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.


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



Relevant Pages

  • Re: PHP Performance problem after upgrade to 5.1.6 or 5.2.0
    ... I have performance problem with PHP after upgrading from PHP 5.1.4 to newer version regardles if newer version is 5.1.6 or 5.2.0. ... everything is fine and load is about 1.5. ...
    (freebsd-performance)
  • Re: [PHP] PHP Performance and System Load
    ... General performance tip do not include more than 10 files a page load. ... Uses php Adodb ... At the moment I am trying to lower the number of filesystem calls as ... over a symlink placed in every webspace directory. ...
    (php.general)
  • Re: High Scores
    ... If you want to view High scores it returns Could Not load scores ... here is the PHP script ... Edit the game Flash file to point to this file on your server ... >> And the PHP script is here ...
    (alt.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. ... namespace collision is not a problem PHP ... > programmers' tendency to write rubbish documentation. ...
    (comp.lang.php)
  • Re: about structure.
    ... Of course if you want to use a load of checkboxes, it works too, just modify ... I thought about using a database... ... "I thought about using php... ... server needs to call upon php... ...
    (alt.php)