php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #29480 __autload() in classes
Submitted: 2004-08-01 14:39 UTC Modified: 2005-05-02 11:56 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: roethlis at iam dot unibe dot ch Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 5.0.0 OS: irrelevant
Private report: No CVE-ID: None
 [2004-08-01 14:39 UTC] roethlis at iam dot unibe dot ch
Description:
------------
It would be great, if one can use __autoload($class) also as a method in classes, not only as a function in global scope. So __autoload should be available in a class, like __get(), __set() or __call() is. It's often needed that you can communicate with an object of another class inside your current class, for that purpose, it's necessary that the class definition of this other class is loaded, which could be achieved using __autoload() inside a class. 

Because many classes need to have access to a general purpose class like Collection or Object, you have to write a lot of require_once "class.Any.php" at the top of many classes, because you never know, if Any is already loaded or not. It would be much better, if you could put require_once "class.Any.php" in the __autoload() method of this class, so you can be sure, that require_once is only executed when it's really necessary.

Sure, it would be possible to have __autoload() in global scope, but because I have all related classes in the same directory, I could simply say require_once "myClass.php", when __autoload sits directly in a class. When it's in the global scope, maybe in another included file, I would have to specify the path to myClass.php, which make it difficult to move classes around in a large class system. 

kind regards,
Dave


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-05-02 11:56 UTC] andrey@php.net
The make few scripts that include most important classes like Object and Collection. For more advanced usage consider using spl_autoload_register() and the advanced SPL's autoload mechanism.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 11:01:33 2024 UTC