[PATCH] Namespace Patch, Beta 1

From: Date: Tue, 02 Aug 2005 04:07:03 +0000
Subject: [PATCH] Namespace Patch, Beta 1
Groups: php.internals 
Request: Send a blank email to internals+get-17484@lists.php.net to get a copy of this message
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. - 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, 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. Namespace-private classes ------------------------- Classes marked as "private" from within a namespace can only be used by classes defined in the same namespace. Anonymous namespaces -------------------- Classes defined inside anonymous namespaces can only be used within the file that declared the anonymous namespace. I believe the attached patch solves the issues that were presented long ago for namespaces. Referring to the post by Daniel Cogwill at http://www.zend.com/lists/engine2/200304/msg00164.html, the following are my answers to the problems he found at the time: 1) import-all silently hides local names. A: If a global class is defined with the same name as a class in a namespace, and the namespace class has been imported in the same script, the global class takes precedence. The namespace class can still be used by using its full name, i.e. namespace_name:class_name. 2) Interestingly (and I assume this is simply a bug), functions and classes behave differently wrt to import-all and duplicate definitions. A: Irrelevant in this patch. 3) There is no way to hide names from import (i.e. make a name non-exportable). This is a fundamental namespace feature, and there is already an appropriate keyword (private). A: Namespace-private classes are supported, using the "private" keyword. 4) The semantic meaning of successive imports depends on their relative ordering. A: The engine does not attempt to determine which class is included, the user decides the most appropriate behavior inside __autoload. 5) The most egregious issue is that an import in an included file affects the scope of the including file. A: Imports in this patch only affect the file they are in. Since I could not find a way to add the tests/classes/namespace directory and subdirectories, I zipped them separately, as before. Let me know if you have any questions on the attached patch. Best regards, Jessie Hernandez

Attachment: [text/x-diff] ns_0801.patch
Attachment: [application/x-gzip] tests.tar.gz
« previous php.internals (#17484) next »