I just thought of something that might be a solution to the lookup rules that we currently have in the namespaces implementation. Whether it's good or not, I just wanted to throw it out there. Here goes:

Support a user-defined function named __get_namespace_classes, which will be similar to __autoload. The signature of the function would be the following:


array __get_namespace_classes(string $namespaceName);

Returns an array of names of classes that are under the specified namespace, or FALSE if the classes under the namespace could not be determined.


The above function would be used in the lookup rules as follows (using DateTime as an example):

1) Does the class DateTime exist in the current namespace?
2) If not, and the function __get_namespace_classes exists, call __get_namespace_classes. 3) If the string DateTime is returned in the array from __get_namespace_classes, then autoload that class. 4) If the class is not in the resulting array, or if the result was FALSE, then look for an internal class DateTime.


With the above function, you can define classes inside your namespace that are named the same as internal classes without having to explicitly "use" them all over the place. You also solve the problem of namespace imports (sorry, use :-) ):

<?php
use PEAR::Net::*;

$a = new FTP();

// The above "use" statement results in calling the
// __get_namespace_classes function and "using" each class in the
// resulting array internally. Meaning the above will be equivalent to
// this:
//
// use PEAR::Net::Curl;
// use PEAR::Net::DNS;
// use PEAR::Net::FTP;
// ...etc...
?>


For the above function to work best, the PHP "dir" function, as an example, should be modified to have an additional "use_include_path" argument:

object dir(string $directory [, bool $use_include_path])


By passing TRUE as the second argument, the directory will be searched for in the include path. The user can then do something like the following as an implementation of __get_namespace_classes (assuming the user organized it into class per file, as is common):

<?php
function __get_namespace_classes($namespaceName)
{
    $classes = false;
    $nsDir = str_replace( '::', DIRECTORY_SEPARATOR, $namespaceName );
    $d = dir( $nsDir, true );

    if( $d )
    {
        $classes = array();

        while( ( $file = $d->read() ) !== false )
            $classes[] = str_replace( '.php', '', $file );

        $d->close();
    }

    return $classes;
}
?>


Let me know what you think!



Regards,

Jessie

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to