[PATCH] Namespace Patch, Alpha 3
| From: | Jessie Hernandez | Date: | Wed, 20 Jul 2005 04:05:30 +0000 |
| Subject: | [PATCH] Namespace Patch, Alpha 3 | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-17332@lists.php.net to get a copy of this message | ||
Private class support has been completed in the attached patch! Again, to
reiterate from my last post, the attached patch is a CVS patch, as some
requested me to do. I could not add the test files to the patch because
"cvs add" failed with a "cvs add requires write access to the repository"
error (any way to fix this?).
Also, import statements now include the class files, using a new .ini
variable, "class_path". Files under the directories in the class_path have
the namespace names as directories and the class names named exactly as the
file that declares it (like Java), e.g. my_ns:class1 is declared in
$class_path/my_ns/class1.php.
Again, the only missing feature I know of is "namespace imports". I've been
thinking about it, and I think the best approach is actually very simple.
Right now, when an undefined class is found, the __autoload function is
called from zend_lookup_class to attempt to declare the class at that
point. What I propose is to modify zend_lookup_class to do an additional
lookup, either before/after the __autoload function, for namespace imported
classes. Here's an example:
<?php
import namespace my_namespace1;
// my_namespace1 is added to a runtime hashtable
import namespace my_namespace2;
// my_namespace2 is added to a runtime hashtable
$a = new class1();
?>
At compile-time, the "my_namespace1" and "my_namespace2" strings are saved
in a runtime hashtable. At runtime, zend_lookup_class will be called on
"class1", as usual. Before/after __autoload, this hashtable will be
traversed for the file "class1.php". So in the above example, an attempt to
include "$class_path/my_namespace1/class1.php" will be done. If that fails,
then my_namespace2 is tried, and so forth. If one of these attempts
succeed, then the import alias is also added to the import hashtable for
the currently-executed file, as if the user had added "import
my_namespace1:class1" to the script.
(BTW, is there a way to construct an opcode at runtime and execute it
immediately? If not, I'll have to duplicate the code I have for
ZEND_IMPORT_CLASS in another function).
I think this approach is the easiest and most sensible for namespace import
support. It is no more than a specialized "__autoload". If there are no
objections, I'll start working on it next week. As always,
comments/suggestions are most appreciated.
Regards,
Jessie Hernandez
Attachment: [text/x-diff] ns_0719.patch
Attachment: [application/x-gzip] tests.tar.gz
Attachment: [text/x-diff] ns_0719.patch
Attachment: [application/x-gzip] tests.tar.gz