note 62723 deleted from language.oop5.autoload by crell
| From: | crell@php.net | Date: | Mon, 04 Apr 2022 16:05:08 +0000 |
| Subject: | note 62723 deleted from language.oop5.autoload by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-218011@lists.php.net to get a copy of this message | ||
Note Submitter: RQuadling at GMail dot com
----
An issue I've had with using the __autoload function is getting it into the application.
You have to have the function included in every topmost script. This is a pain if the entire
application is OOP and an "app" can be just a component of another "app".
A solution I've found is to use php.ini's auto_prepend_file setting.
Mine is set to ...
auto_prepend_file = auto_loader.php
The auto_loader.php script contains a single function. The __autoload() function.
The include_dir path IS examined to find this file, so you can just put it with the rest of your
includable files.
A useful additional facility here is that you could log which classes are used by a script at
runtime. Very useful if you have object factories and can't know the load at design time.
Also, assigning the uncaught exception handler and the error handlers in this file means your entire
site WILL have some global protection without you having to deal with it on a script by script
basis.
If you do not have access to the PHP.INI file, or you are running on a shared server, you may not be
able to set this property. In those cases, you may be able to set the value using .htaccess. (NOTE:
UNTESTED as I don't use Apache).
<IfModule mod_php5.c>
php_value auto_prepend_file "auto_loader.php"
</IfModule>
You COULD therefore have a different set of rules per subdomain (if you have multiple subdomains,
say, live, test, beta, devel) or whatever takes your fancy.
For more details on this see the "Description of core php.ini directives"
(http://www.php.net/manual/en/ini.core.php)