I’ve discovered the __autoload function today and after reading the official manual page of this function there’s a point I don’t get at all.
What’s clearly the difference between using __autoload() and let’s say require_once?
because it looks autoload is the new fashion way of doing the required includes but to me it’s far better to use require_once instead. Hence, __autoload has to be defined into all the php files which means writing its code down there, if I put ALL my includes/require_once/…etc into one file let’s call it main_header.php then all I’ll need to do in my web app files is write one line of code:
<?php require_once('main_header.php'); ?>
Am I wrong somewhere?
I can see two things going for autoloading (not necessarily
__autoload; prefer the more modernspl_autoload_registerinstead):main_header.phpas in your example, but then the next item comes into effect.It’s also worth to point out that autoloading is also triggered when unserializing an object of a class that has not been yet defined, which makes things infinitely more practical. Of course there is another hook in
unserializefor this (the configuration settingunserialize_callback_func), so autoloading is not technically necessary. It’s definitely nicer though.