Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:34057 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 42291 invoked by uid 1010); 17 Dec 2007 09:46:13 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 42264 invoked from network); 17 Dec 2007 09:46:13 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 17 Dec 2007 09:46:13 -0000 Authentication-Results: pb1.pair.com header.from=dmitry@zend.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=dmitry@zend.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain zend.com designates 212.25.124.162 as permitted sender) X-PHP-List-Original-Sender: dmitry@zend.com X-Host-Fingerprint: 212.25.124.162 mail.zend.com Linux 2.5 (sometimes 2.4) (4) Received: from [212.25.124.162] ([212.25.124.162:52079] helo=mail.zend.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id D2/81-23767-15546674 for ; Mon, 17 Dec 2007 04:46:10 -0500 Received: (qmail 23421 invoked from network); 17 Dec 2007 09:45:43 -0000 Received: from internal.zend.office (HELO ?127.0.0.1?) (10.1.1.1) by internal.zend.office with SMTP; 17 Dec 2007 09:45:43 -0000 Message-ID: <47664546.3030107@zend.com> Date: Mon, 17 Dec 2007 12:45:42 +0300 User-Agent: Thunderbird 2.0.0.9 (Windows/20071031) MIME-Version: 1.0 To: Jessie Hernandez CC: internals@lists.php.net References: <97.BF.38526.B657F574@pb1.pair.com> <4A.C3.44650.82DA2674@pb1.pair.com> In-Reply-To: <4A.C3.44650.82DA2674@pb1.pair.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Re: Idea for namespace lookup resolution From: dmitry@zend.com (Dmitry Stogov) Hi Jessie, The namespace may include several files and it may be extended with additional files in any moment. So having single __get_namespace_classes() function will require to update it every time you extend namespace. Also such function will significantly slowdown access to internal classes. (It was the main reason why __autoload() was moved after check for internal class). Thanks. Dmitry. Jessie Hernandez wrote: > Attached is the proof-of-concept patch. If the idea is met well, then > I'll keep working on it to add caching and, if there's interest, I'll > add *namespace imports*. > > An example of how a class in a namespace that's named the same as an > internal class can be loaded: > > autoload.php > function __autoload($className) > { > include_once str_replace( '::', DIRECTORY_SEPARATOR, $className ) . > '.php'; > } > > function __get_namespace_classes($namespaceName) > { > return array( 'Exception' ); > } > ?> > > test.php > namespace ns; > > include 'autoload.php'; > $ex = new Exception(); > var_dump( $ex ); // prints out ns:Exception instead of Exception > ?> > > > I ran a few tests on it and did not find any significant performance > hit. If possible, I'd like someone to try it out and see if they have > the same result. > > As always, comments/suggestions on the patch are welcome. > > > Regards, > > Jessie Hernandez > Zend Certified Engineer (http://zend.com/zce.php?c=ZEND006359&r=222727282) > > > Jessie Hernandez wrote: >> I just thought of something that might be a solution to the lookup >> rules that we currently have in the namespaces implementation. Whether >> it's good or not, I just wanted to throw it out there. Here goes: >> >> Support a user-defined function named __get_namespace_classes, which >> will be similar to __autoload. The signature of the function would be >> the following: >> >> >> array __get_namespace_classes(string $namespaceName); >> >> Returns an array of names of classes that are under the specified >> namespace, or FALSE if the classes under the namespace could not be >> determined. >> >> >> The above function would be used in the lookup rules as follows (using >> DateTime as an example): >> >> 1) Does the class DateTime exist in the current namespace? >> 2) If not, and the function __get_namespace_classes exists, call >> __get_namespace_classes. >> 3) If the string DateTime is returned in the array from >> __get_namespace_classes, then autoload that class. >> 4) If the class is not in the resulting array, or if the result was >> FALSE, then look for an internal class DateTime. >> >> >> With the above function, you can define classes inside your namespace >> that are named the same as internal classes without having to >> explicitly "use" them all over the place. You also solve the problem >> of namespace imports (sorry, use :-) ): >> >> > use PEAR::Net::*; >> >> $a = new FTP(); >> >> // The above "use" statement results in calling the >> // __get_namespace_classes function and "using" each class in the >> // resulting array internally. Meaning the above will be equivalent to >> // this: >> // >> // use PEAR::Net::Curl; >> // use PEAR::Net::DNS; >> // use PEAR::Net::FTP; >> // ...etc... >> ?> >> >> >> For the above function to work best, the PHP "dir" function, as an >> example, should be modified to have an additional "use_include_path" >> argument: >> >> object dir(string $directory [, bool $use_include_path]) >> >> >> By passing TRUE as the second argument, the directory will be searched >> for in the include path. The user can then do something like the >> following as an implementation of __get_namespace_classes (assuming >> the user organized it into class per file, as is common): >> >> > function __get_namespace_classes($namespaceName) >> { >> $classes = false; >> $nsDir = str_replace( '::', DIRECTORY_SEPARATOR, $namespaceName ); >> $d = dir( $nsDir, true ); >> >> if( $d ) >> { >> $classes = array(); >> >> while( ( $file = $d->read() ) !== false ) >> $classes[] = str_replace( '.php', '', $file ); >> >> $d->close(); >> } >> >> return $classes; >> } >> ?> >> >> >> Let me know what you think! >> >> >> >> Regards, >> >> Jessie >