Re: User namespaces and PHP classes
| From: | Stan Vassilev | FM | Date: | Wed, 01 Apr 2009 11:04:33 +0000 |
| Subject: | Re: User namespaces and PHP classes | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-43572@lists.php.net to get a copy of this message | ||
Hi,
There is a way to implement in some future release of PHP (without hurting performance), but it's not in the scope of 5.3.
Each namespace should have a meta file listing all symbols present in it. The parser loads that static file at parse time and resolves all naming at parse time, leaving autoload to figure out only where a symbol is, not whether it's present.
I've not noticed great interest in this approach last time I offered it.
Regards,
Stan Vassilev
Hi, So definetly I need to prepend the \ or declare the "usage" of the class at the beginning in order to use classes declared in the global scope? This means SPL classes and 3rd-party classes. Is there a way to import all the SPL classes at once? ;) (use SPL\*, use PDO\*).. just wondering. Thanks On Tue, Mar 31, 2009 at 5:38 PM, Stan Vassilev | FM <sv_forums@fmethod.com> wrote:Hi, Try this: use PDO; Regards, Stan Vassilev ------------------------------------------------------------------------------------- Hello, I've been writing in the last days a web application on PHP 5.3 (beta1 although RC was released) cause of all the goodies it brings, specially the namespaces, however I've been a bit stuck and *surprised* that the only way to use a PHP class (like Iterators, PDO, etc) is to call them from the global scope, this means: adding the \ at the beginning at the class. My short question is: is it the only possible way? That means that I need to be very careful when invoking PHP classes, like PDO? Currently when I do: $pdo = new PDO(..); throws an error if I use it from one of "my" namespaced-classes, the error is: Fatal error: Class 'MyNameSpace\PDO' not found in /foo/bar/foome.php throws an error, since spl_autoload can't find the 'MyNameSpace\PDO' class. And yes I know I could create a class that extends from it, but I don't think that's the best idea, just imagine the number of classes I just need to create to cover the PHP classes I plan to use. So, are there other workarounds for this? Like an autoload function for PHP objects created inside a non-global scope (MyNameSpace)? I was reading a thread about namespaces of last year and someone suggested something like: if (($p = strrpos($class, '\\')) !== false) { $name = substr($class, $p+1); if (class_exists("\\$name")) { spl_autoload("\\$name"); // or use "\\$name"; return; } } However that wont work cause: 1) one can't use "use" at the middle of a PHP files, it needs to be used at the beginning afaik, 2) spl_autoload will definetly find the class I'm passing (supposing PDO) but it's useless since what is "expected" by PHP is a MyNameSpace\PDO class. Probably I could use Reflection but I'm not sure if that's the best idea... Any other plans to use PHP objects (SPL, PDO, etc) inside non-global namespaces? Maybe a "PHP" namespace that spl_autoload could catch, like: use PHP; use PHP\SPL; use PHP\ArrayObject; (this idea was given in a previous thread :-)) .. or perhaps having something like spl_namespace_alias, when someone can use inside their autoload functions: spl_namespace_alias('/PDO', $classname); // where $classname is the parameter received by the __autoload functions. I don't have any problems to prepend the "\" but I think PHP should catch that stuff automatically as it does with constants and functions, no? Find a bit annoying to write the "\" to PHP classes I want to use imho. Thanks! -- Pablo Fischer (pablo [arroba/at] pablo.com.mx) -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php-- Pablo Fischer (pablo [arroba/at] pablo.com.mx) -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php