__autoload proposal

php.internals

Wojciech Malota

18 years ago
I have a proposal for prototype of __autoload function in PHP 5.3.0. In this version of PHP namespaces will be available. Prototype of __autoload could look like this: __autoload($classname, $namespace = null); Now __autoload have only one argument which is a name of searched class. Second argument could be a namespace where class is searched. It should have default value for backward compatibility. For example: <?php namespace Space1::Space2; class A { public function Test() { $b = new B; // Class B isn't known at this moment } } ?> When user try to create new object of unknown class B __autoload function should be called with parameter $classname = 'B' and $namespace = 'Space1::Space2' but int this example: <?php namespace Space1::Space2; class A { public function Test() { $b = new Space3::Space4::B; // Class B isn't known at this moment } } ?> Arguments should be: $classname = 'B' and $namespace = 'Space3::Space4'; I think it is the most flexible solution. It allows to create very useful and effective mapping mechanisms.
-- Wojciech Ma³ota

David Coallier

18 years ago
On Dec 22, 2007 3:43 PM, Wojciech Malota <wojtekm86@konto.pl> wrote: > I have a proposal for prototype of __autoload function in PHP 5.3.0. > In this version of PHP namespaces will be available. > Prototype of __autoload could look like this: > > __autoload($classname, $namespace = null); > > Now __autoload have only one argument which is a name of searched class. > Second argument could be a namespace where class is searched. It should have > default value for backward compatibility. > > For example: > > <?php > namespace Space1::Space2; > class A { > public function Test() { > $b = new B; // Class B isn't known at this moment > } > } > ?> > > When user try to create new object of unknown class B __autoload function > should be called with parameter $classname = 'B' and $namespace = > 'Space1::Space2' > > but int this example: > > > <?php > namespace Space1::Space2; > class A { > public function Test() { > $b = new Space3::Space4::B; // Class B isn't known at this moment > } > } > ?> > > Arguments should be: $classname = 'B' and $namespace = 'Space3::Space4'; > > I think it is the most flexible solution. It allows to create very useful > and effective mapping mechanisms. > > -- > Wojciech Ma³ota > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > Patch ? -- David Coallier, Founder & Software Architect, Agora Production (http://agoraproduction.com) 51.42.06.70.18

Stanislav Malyshev

18 years ago
> Now __autoload have only one argument which is a name of searched class. > Second argument could be a namespace where class is searched. It should have > default value for backward compatibility.
What for? It always receives full name of the class, whatever it is.
> I think it is the most flexible solution. It allows to create very useful > and effective mapping mechanisms.
I don't see any reason to complicate the API - if you need it in your autoload, you can easily split the full name into parts before and after the final :: and achieve the same effect.
-- Stanislav Malyshev, Zend Software Architect stas@zend.com http://www.zend.com/ (408)253-8829 MSN: stas@zend.com