Skip to content

Instantly share code, notes, and snippets.

@mhull mhull/.Autoload.php
Last active Mar 31, 2018

Embed
What would you like to do?
Demonstrates how to autoload classes in PHP, so that we don't have to explicitly require each file for each class used within our code
<?php
/**
* Uses the `spl_autoload_register` function to autoload classes so that we don't have to
* explicitly require each file for each class we use in our code
*/
<?php
/**
* This looks silly, don't you agree?
*/
# make sure we have all the classes we might need, just in case!
require_once 'My_Class.php';
require_once 'My_Second_Class.php';
require_once 'My_Third_Class.php';
require_once 'My_Fourth_Class.php';
require_once 'My_Fifth_Class.php';
require_once 'My_Sixth_Class.php';
# ... somewhere within our code, use My_Class
$my_object = new My_Class();
# ... somewhere else in our code, use My_Fifth_Class
$my_fifth_object = new My_Fifth_Class();
# ... etc ...
<?php
/**
* Prior to PHP 5.1.2, autoloading basically worked like this.
*
* Note that this method only lets us have one function to specify autoloading behavior, which
* will certainly not work within a larger PHP framework with multiple components that may
* be written by multiple people.
*/
# define a function to autoload a file based on the class name being instantiated
function __autoload( $class_name ) {
/**
* Note we are just looking for a PHP file with the same name as the class in question.
* actual usage may require some string operations to specify the filename
*/
$file_name = $class_name . '.php';
if( file_exists( $file_name ) ) {
require $file_name;
}
}
/**
* Now, we can instantiate a class without explicitly requiring the corresponding file. Note
* we would need to have a file called `My_Class.php` in the same directory as this script.
*/
$my_object = new My_Class();
<?php
/**
* Example 1: Using an anonymous function as the single parameter for `spl_autoload_register`
*
* @see http://php.net/manual/en/functions.anonymous.php
*/
spl_autoload_register( function( $class_name ) {
/**
* Note that actual usage may require some string operations to specify the filename
*/
$file_name = $class_name . '.php';
if( file_exists( $file_name ) ) {
require $file_name;
}
} );
/**
* ... anywhere in our code, we can now get a new My_Class object without caring whether
* the class's file has been required or not.
*
* Note in this case we need to have a file called `My_Class.php` in the same directory as this script.
*/
$object = new My_Class();
$object->do_stuff();
<?php
/**
* Example 2: Using a string as the single parameter for `spl_autoload_register`
*
* We need to define a function with the same name as the string we provide
*
* @see http://php.net/manual/en/language.types.callable.php
*/
spl_autoload_register( 'my_autoload_register' );
/**
* This function has the same name as the string we passed in above, and acts
* exactly the same as the anonymous function from Example 1.
*/
function my_autoload_register( $class_name ) {
/** etc **/
}
<?php
/**
* Example 3: Using additional parameters for `spl_autoload_register`
*
* @param $throw = true Optionally turn off exception throwing
* @param $prepend = false Optionally add our function to the beginning of the autoload queue
*/
/**
* Between the two functions being registered, `other_autoload_register` will get called first, even
* though it is being registered second, because it uses the $prepend parameter.
*/
spl_autoload_register( 'my_autoload_register' );
spl_autoload_register( 'other_autoload_register', null, true );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.