Re: [PATCH] Namespace Patch, Alpha 3
| From: | Jessie Hernandez | Date: | Tue, 26 Jul 2005 15:50:35 +0000 |
| Subject: | Re: [PATCH] Namespace Patch, Alpha 3 | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-17409@lists.php.net to get a copy of this message | ||
Hi Marcus,
"Marcus Boerger" <mail@marcus-boerger.de> wrote in message
news:1374121835.20050726101045@marcus-boerger.de...
>
> > 3) Simply pass an extra argument (or pass an array or object as the
first
> > argument) to __autoload containing the names of the imported namespaces
of
> > the calling file. __autoload can then use this array against the class
name
> > and perform its own lookup.
>
> None of the above makes any sense. Look __autoload() is just bein called
at
> any place where a class is missing. So looking for the file first and then
> loading __autoload(I) makes no sense whatsoever. IMO what we need here is
a
> default implementation for import behavior accomplished by __autoload().
So
> for example "import ns:cls;" leads to "__autoload('ns:cls')".
> Here the SPL
> default implementation would relpace all ':' with the directory divider
'/'
> or '\', append '.inc' or '.php' or '.inc.php' and
> append the result to all
> directories in the include_path to look for the file. If were are to
support
> import with '*' the the autoload implementation would need to take care of
> this and load all matching files. A user land implementation can easily
> achieve that by using SPLs (Recursive)DirectoryIterator and
FilterIterator.
>
Regarding a fully-specified class import, such as ns:cls, if I simply remove
my include logic, then __autoload('ns:cls') would automatically be called.
There are no issues with this type of import.
Now, namespace imports (wildcard imports) are a different story. Maybe I did
not make my explanations clear enough, but you basically agreed to point #3
in my previous email. Now, the class name cannot simply be passed to
__autoload, because there is no way for __autoload to know to which
namespace the class belongs to, and you only want __autoload to include the
classes belonging to the namespaces that have been imported. That is why the
list of imported namespaces for the __calling__ file must be accessible
somehow to the __autoload function, either as an additional argument or by
having the first argument be an array/object.
So the namespace imports will work like how you described simple imports,
but with a slight twist. To quote you:
"Simple" Imports
----------------
> So for example "import ns:cls;" leads to "__autoload('ns:cls')".
> Here the
SPL
> default implementation would relpace all ':' with the directory divider
'/'
> or '\', append '.inc' or '.php' or '.inc.php' and
> append the result to all
> directories in the include_path to look for the file.
By slightly modifying your sentence, we implement namespace imports:
Namespace Imports
-----------------
Here the SPL default implementation would, in a loop, prefix the class name
with each imported namespace, replace all ':' with the directory divider '/'
or '\', append '.inc' or '.php' or '.inc.php' and append the
result to all
directories in the include_path to look for the file.
For example, here is how the userland __autoload might look (notice that
this code is exactly what I provided in the last post). In this example, the
user has the directory structure as "namespace-name/class-name.php".
<?php
function __autoload($className, $importNamespaces)
{
$paths = explode( PATH_SEPARATOR, get_include_path() );
foreach( $paths as $path )
{
foreach( $importNamespaces as $ns )
{
$fullPath = $path . DIRECTORY_SEPARATOR . str_replace( ':',
DIRECTORY_SEPARATOR, $ns ) . DIRECTORY_SEPARATOR . $className . '.php';
if( file_exists( $fullPath ) )
{
require_once( $fullPath );
break;
}
}
}
}
?>
Let me know if the above make sense or if you see any problems with this
approach.
--
Jessie