note 56979 added to language.oop5.autoload
| From: | kai at kaiundina dot de | Date: | Tue, 20 Sep 2005 19:34:58 +0000 |
| Subject: | note 56979 added to language.oop5.autoload | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-95499@lists.php.net to get a copy of this message | ||
To: gomasj at gmail dot com
I strongly disagree in that "no well organized programmer will need to use it".
The method reduces the overhead when loading unneeded classes while encouraging the use of well
organized class libraries.
Assume you have a class needing an undetermined number of other classes to operate properly -
let's say an XML-parser that parses a document into an object tree (one class per element is a
typical procedure).
A common behavior was to load all possibly needed classes in advance - regardless if they would be
used ever. That would be a performance deasaster for the above case.
Another possibility was to load the particular class when needed - providing a path to the file
containing the class declaration. Since relative path's in PHP are calculated relative to the
script handling the client request, they're some kind of complex to manage in huge projects,
where the script can be called from various locations.
If you're willing to move a class's declaration file to another location (directory) you
have to update each reference to that file.
Lot's of well organized programmers (so are you), wrote their own mechanisms so solve these
problems. But you always had to make at least one call to load the class file each time it might
possibly be referenced for the first time in code execution - that are:
- new
- extends
- implements
- Type hints
- static reference (class::xxx)
- deserialization of sessions
__autoload() simply provides an event handler to centralize the loading of a class's
declaration only when it's needed for the first time. Regardless where or by what the request
occurred.
So it keeps your code clean, fast and allows you to reorganize your class-library as you like
without modifying any of your source files (except the autoload-function itself).
We use the autoload feature in a 200,000 lines php-project as follows:
The 'ClassManager' maintains a cache file containing the file paths to each
class-declaration. Whenever an unknown class (not present in the cache, yet) is requested, it
searches the directories for a file containing the desired class declaration and adds it to the
cache for the next time. This method allows us to modify our class-library at will. We can add, move
or remove classes without adjusting a single line of code. When our software starts up, the
ClassManager has to handle about 6,000 class requests - without having to worry about performance
reasons.
I am developer and project manager in the project mentioned above (a web based cms) - so I'd
call myself well organized.
Anyway,
thanks for your opinion
----
Manual Page -- http://www.php.net/manual/en/language.oop5.autoload.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+56979
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56979&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56979&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56979&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56979&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56979&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+56979&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56979&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+56979&report=yes
Search -- http://master.php.net/manage/user-notes.php