__autoload vs include family

Viewed 3733

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?

8 Answers

Another note: include and require may become a big pain while unit testing.

I don't know how should I prevent requiring / including anything. There is test helpers, that can do some useful things, such as preventing exiting or dieing, but they still cannot mock include or require.

Mockery library recommends using autoloading to be able to mock public static methods.

Related