简体   繁体   中英

__autoload vs include family

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 modern spl_autoload_register instead):

  1. You don't need to explicitly include the classes. Of course, you could make a main_header.php as in your example, but then the next item comes into effect.
  2. You don't have to load 100 classes if you are only going to use 10 of them.

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 unserialize for this (the configuration setting unserialize_callback_func ), so autoloading is not technically necessary. It's definitely nicer though.

First: Use spl_autoload_register() instead of __autoload() .

The autoloader is called, when you try to access a class, that doesn't exists. With include() you just include a class. The main difference is, that using an autoloader the class is only included, if its really required/used.

Usually you define an autoloader in one file and include this one on every request. If you use bootstrapping (meaning: a single file, that catches every request and redirects it to the appropriate target) its only required to define the autoloader there. So its its not required to define it in every file.

Autoloader is used for lazy initialization. It's the most effective with MVC architecture, not with websites that include a file here and there and define db connection string in every file (which is terrible).

Using autoload with MVC framework saves resources and brings a lot to modularity. Since you don't have to include files with classes, you just instantiate a class you require in the controller you're currently at.

Basically, it's an OOP thing. You shouldn't worry about it if you're not using object approach to structuring your website and include/require is what will work for you.

Choose the right tool for the job. Autoloading is for loading the source-code of yet undefined Classes prior use. So you can instantiate classes you have not required the files for so far.

This makes sense if you work with classes and you want to require files at a central place, a single point instead all over in different files.

But if you're using includes and requires to let's say build a website with menu and footer, this does not make sense. Autoload does not work for this:

<html>
  <head><title>Reciepes collection - My Homepage</title></head>
  <body>
<?php include('header.html'); ?>
<h1>My Reciepes collection</h1>
<p>Cooking is one of my favorite hobbies .... (</p>....)
<?php include('footer.html'); ?>
  </body>
</html>

If you're making use of requires to load functions, you can not use autoload either.

So only because autoload looks somewhat fancied by others to you, it does not mean that it fits your needs.

I personally use it in projects where I do not want to take care of the requires for classes and where I want to be able to dynamically load classes from within a modular directory and library structure.

I only make use of spl_autoload_register() to register one or more autoload implementations. It's the recommended method to register an autoload function in PHP as it allows to have multiple autoloaders.

I think it comes down to personal preference. For me, autoload is a typical php kind of thing - a dirty "magic-quotes"-alike hack, that only exists because they don't bother to sit down and code a clean solution to the problem - in this case, an adequate packaging system + a catchable ClassNotFound exception. Until we've got this fixed, I'll stick to include .

All the answers are more than correct and adequate. I just thought I should add my two cents.

First, *_once functions are slow. Do not use them.

Secondly, in an app I am developing, I have lots of classes which I rely on each other and I do not always know which classes are needed in a page.

Instead of creating a main.php page and including all the classes which are a lot; obviously wasting resources and time (Include and require are expensive), I use autoload to include those files as and when they are needed by the page. All I do therefore is code and the classes are added automatically.

This also improves your code and directory structure since you follow a system in naming your classes and files.

Just an addition. In terms of resource usage and speed(decreasing order of requirements and increasing order of speed):

Require_once() ; include_once() ; require() ; include()

Making include the best and fastest.

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.

Autoload and the include family functions both have their place.

Autoload is intended for lazy loading of modules in an OO based script (namely classes and interfaces) and is invoked when you use new or statically call a method of a class that hasn't yet been loaded. This triggers autoload to run a function that attempts to load (typically via include) the class or interface in question. The advantage of this approach is that classes are only loaded as and when they are needed. Any class that doesn't get used doesn't get loaded.

Autoload cannot be used for loading program fragments that aren't a class or an interface, for those items you still need the include family.

As for having to add an autoload function to every script that needs to autoload? In this case you put the autoload function in its own file and include it in the scripts that need to autoload. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM