You don’t need to include() files in PHP

Autoloading was introduced in PHP 5 to give developers a simple way to include classes from other files without having to worry about putting include statements everywhere. Take a look at this simple application below. include_once() is used to include the two files containing the User class and the Account class.

<?php
//index.php
include_once('User.php');
include_once('Account.php');

$user = User::set_current_user();
$account = Account::set_current_account();

$output = 'Welcome to a simple PHP page.<br>';
$output .= 'You are logged in as: '.$user->get_name().'<br>';
$output .= 'Your account number is: '.$account->get_external_number().'<br>';
echo $output;
?>
<?php
//User.php
class User{
protected $name;

static function set_current_user(){
$user = new User();
$user->name = 'Jeff Schaefer';
return $user;
}

function get_name(){
return $this->name;
}
}
?>
<?php
//Account.php
class Account{
protected $number;

static function set_current_account(){
$account = new Account();
$account->number = '12345';
return $account;
}

function get_external_number(){
return $this->number;
}
}
?>

The require_once() functions can be removed from index.php and replaced by an autoloader. See this new version of index.php

<?php
//index.php
function simple_autoload($class){
$filename = ucfirst($class).'.php';
if(file_exists($filename)){
include_once($filename);
}
}
spl_autoload_register('simple_autoload');

$user = User::set_current_user();
$account = Account::set_current_account();
 
 $output = 'Welcome to a simple PHP page.<br>';
 $output .= 'You are logged in as: '.$user->get_name().'<br>';
 $output .= 'Your account number is: '.$account->get_external_number().'<br>';
 echo $output;
?>

When a class name is used but PHP does not recognize it as a class that exists, PHP will check to see if an autoload function has been registered before throwing an error. The autoload function above takes the class name as a parameter and creates a filename string by appending .php to the class name. Then it checks if that file exists, and includes it. If it does not exist, then when the function is returned PHP will still not be able to find the class and an error will be thrown as usual.

There are two functions that define autoloaders. __autoload() takes the name of the function as a parameter and defines only one autoloader. This is a shortcoming and it is recommended to always use spl_autoload_register() instead. spl_autoload_register also takes the name of the function as a parameter but it allows you to register multiple functions. The autoloaders will be run in the order they are registered until the class is found, or until all of the autoloaders fail. To register multiple autoload functions you must call spl_autoload_register(‘function_name’) separately for each function.

The function above depends on all of the files being in the same directory. In the real world this may not be the case, so your autoloader should include an absolute path to the location of your files. A more advanced example could also look in multiple directories. Instead of defining and registering the autoload function in index.php we could define it in its own file, and include that first when execution begins.

<?php
//index.php
include_once('Autoload.php');

$user = User::set_current_user();
$account = Account::set_current_account();

$output = 'Welcome to a simple PHP page.<br>';
$output .= 'You are logged in as: '.$user->get_name().'<br>';
$output .= 'Your account number is: '.$account->get_external_number().'<br>';
echo $output;
?>
<?php
//Autoload.php
define('ROOT_PATH','path\\to\\your\\files');

function better_autoload($class){
$filename = ucfirst($class).'.php';
$subdirectories = array('Model','View');
foreach($subdirectories as $subdir){
$full_filename = ROOT_PATH.'\\'.$subdir.'\\'.$filename;
if(file_exists($full_filename)){
include_once($full_filename);
}elseif(file_exists(ROOT_PATH.'\\'.$filename)){//check in the root path
include_once(ROOT_PATH.'\\'.$filename);
}
}
}

spl_autoload_register('better_autoload');
?>

Now our autoloader will look in the Model and View directories as well as the root directory.

To turn an autoload function off use spl_autoload_unregister(‘function_name’);

To see what functions are currently registered use spl_autoload_functions()

I hope this brief tutorial will take away some of the headaches that come from having to constantly manage includes in PHP.

2 thoughts on “You don’t need to include() files in PHP

Leave a Reply

Your email address will not be published. Required fields are marked *