"John LeSueur" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> >
> so, if I understand correctly, if your __autoload is defined in a
> different file(file1) from where the import was
> called(file2), then get_imported_namespaces() returns the namespace
> imports for file1.
> get_imported_namespaces($className) would return for file2, and outside
> of the __autoload function,
> get_imported_namespaces($className) would return the namespaces for
> file1. That seems like
> complicated semantics. Is there some way this could be simplified? Maybe
> there should be two functions,
> one that returns namespaces for file2 and is incorrect(throws an error)
> to call from anywhere other than
> __autoload(), and one that returns namespaces for file2.
>

To clarify the usage of this function (using your same example of file1 and
file2):

- get_imported_namespaces() can be called in any file. It will return the
namespace imports for the currently-executing file (whether it's file1 or
file2).
- When inside __autoload, get_imported_namespaces($className) will return
the namespace imports for the file that triggered the __autoload call (in
this case, file2).
- When not inside __autoload, get_imported_namespaces($className) will
return an empty array.

I don't think the above usage is complicated. The only thing that can be
changed is the behavior of get_imported_namespaces($className) when not
inside __autoload. Right now it's returning an empty array. Will it be
better to return false and/or generate an error?


> >Imports
> >-------
> >Imports and namespace imports are handled completely by the user with the
> >use of the __autloload function. This means that there are no
restrictions
> >on class file naming/directory structure. For simple imports, the class
> >name will be passed with its full name (including the colons, such as
> >"ns:class1"). For namespace imports, only the class name will be called.
> >Since the user needs to determine which namespace (or namespaces) a class
> >belongs to, the "get_imported_namespaces" function is provided to check
the
> >imported namespaces for the currently-executing file. Once the user is
> >satisfied with a match, he/she needs to perform an "import" for this
class,
> >
> >
> This seems like it would be slow, as the user might have many
> namespaces(directories) to search for matching
> files. This would of course be up to the user. And it doesn't bother me
> that a full namespace import
> may not be as fast as a more specific import. In development it might be
> convenient, but at some point, it's not
> that big a deal to specify the classes you use.
>

This same search would have to be done by the engine, and although a bit
faster, this could also be slow depending on the number of imported
namespaces. Also, if the search is done by the engine, then a file
structure/naming convention would have to be enforced (that's why I had the
class_path .ini variable before), and some already mentioned that they have
different conventions, hence we need to provide flexibility to the user.

I agree with you that it's not a big deal to be explicit in specifying which
classes will be imported (in fact, when I have to write a Java class at
work, I always specify each class one-by-one, regardless of how many imports
I have to do, as it also makes the code clearer).


> >but this needs to be done on the executing file, not the file where
> >__autoload is defined. For this reason, the autoload_import_class
function
> >is provided. A sample usage of both these functions is at
> >tests/classes/namespace_autoload.php.
> >
> >
> I like the option of having namespaces, and from the description this
> seems to be a light method of doing it. It does
> require some effort from the user. It seems that for most cases
> __autoload will be the same every time, maybe with
> different paths or file naming conventions.
>

You are correct, this is a very lightweight implementation. Very few (from
2-4 IIRC, depending on the type of import) hashtables are searched (this is
including class_table), and this number does not increase with the number of
class imports nor with the number of namespace imports.

Right now, it requires some initial effort from the user to implement the
imports, but as you said, this would only need to be done once (once the
__autoload function is defined, this can be included from all other
scripts).

--
Jessie

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

Reply via email to