Lukas Kahwe Smith wrote: > > On 05.11.2008, at 01:05, Stanislav Malyshev wrote: > >> Hi! >> >>> or in other words give the user the ability to specify when he wants >>> the >>> fallback to global and not doing a fallback to global per default. >>> That way >> >> This would be quite complex thing since this way you can't be sure >> which class gets loaded when you say, e.g., Exception - and thus, if >> you write something like "throw new Exception("XML did not load")" >> and except My\XML\Parser\Exception and have catch for it but get just >> Exception your application would happily unroll to the top and fail. >> >> I think actually knowing what class is going to be loaded is a good >> idea and overriding loader behavior so it's asked for one class and >> loads completely different one is not a good idea. One would expect >> if he asks for class X he gets class X, not class Y. > > Well, its not like the person is getting Y when he is expecting X. > Both classes have the same name after all, so there is some relation > between these two classes. More importantly its the users choice to > enable this in __autoload(). As all frameworks got that its the end > users job to implement autoload, I would not worry soo much in this case. > > Just a question: How hard would it be to implement in case we do want > this?
To do in any kind of performant way would require storing the source filename along with the class name in the opcode (not hard), and would also probably best be implemented not just as an autoload callback, but as code that checks a flag in EG() inside zend_fetch_class. Thus, when spl_autoload_register is passed in the dummy resolver name, it would simply set a flag. This would also mean we would have to disallow a chain like so: <?php spl_autoload_register('blah'); spl_autoload_register('__internal__'); // or whatever we decide to call it spl_autoload_register('another'); ?> The last call should throw an error. __internal__ should either be the first or the last registered autoload. Once this is set up, the actual code would simply check for whether the internal class exists. Even though it is technically possible, there is one huge flaw in the idea: autoload can be changed external to the current file. How do you safely design code that would work with both autoload implementations? The truth is that code designed for internal resolution prior to autoload would fail with any other resolution order, and code designed for autoload prior to internal resolution would fail with any other resolution. There is only 1 safe and simple way to do this, and that is to have the same resolution order in all cases, and the only safe resolution is 1) ns::class 2) autoload ns::class 3) fail Greg -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php