Re: [PATCH] Namespace Patch, Beta 1

From: Date: Tue, 02 Aug 2005 09:27:25 +0000
Subject: Re: [PATCH] Namespace Patch, Beta 1
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-17489@lists.php.net to get a copy of this message
Jessie Hernandez wrote:
Attached is the latest version of the namespace patch! It finally includes namespace imports and it even includes anonymous namespace support. Also, the previous bison shift/reduce conflict has been removed. Here is a summary of its features: - Simple imports: import ns:class1; - Import aliases: import ns:class1 as my_alias; - Namespace imports: import namespace ns; - Anonymous namespaces: namespace { class file_class{} } - Namespace-private classes: namespace ns{ private class prv_class{} } Two new functions have also been added to support namespace imports (more on that below): - get_imported_namespaces([$className]) Returns an array of imported namespaces for the current file. If a class name is passed, and this class is currently being autoloaded (also meaning that this function is called inside __autoload), then the list of returned imported namespaces is based on the file that triggered the __autoload call. 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.
- autoload_import_class($className, $namespaceName) Imports a class in a given namespace for the currently executing file (can ONLY be used inside __autoload!) 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.
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. John

« previous php.internals (#17489) next »