How PHP Autoload works
A simple explanation on how auto loading will make your life easier.
Having lots of PHP Classes is common on a large project.
For example, You have a Class that will handle the Database connection, another one will handle Email stuff and so on....
But not all of those PHP Classes are going to be used on a single page in most cases.
For example, The page only shows a static PHP file.
If so then that file wont be using the Database Class, and other not related to the PHP file.
So in this post, we will be talking about how can we not include a Class automatically in a page where it isn't really needed.
Example:
Let's have this as our example Directory and it's files
- css
- js
- images
- lib
- index.php
index.php <php
include 'lib/Connection.php';
include 'lib/Email.php';
echo "I just do echo.";
Connection.php <php
class Connection{
public function __construct(){
echo "I, Connection Class, was called!<br />";
}
public function getSomeDataPlease(){
// will return some Data from Database;
}
}
Email.php <php
class Email{
public function __construct(){
echo "I, Email Class, was called!<br />";
}
}
As you can see from the above example. We included 2 files on our index.php (Connection.php and Email.php)
But index.php isn't using those Classes.
So now, you were thinking of maybe using the Connection class in the index.php.
index.php <php
include 'lib/Connection.php';
include 'lib/Email.php';
$conn = new Connection();
// maybe some function in Connection class that returnsan array from the Database
$data = $conn->getSomeDataPlease();
var_dump($data);
echo "I just do echo.";
Now at least the Connection class is being used here.
Here's a great example of we can create a Connection class PHP PDO or PHP MySQLi
This will be fine.
But thinking in the long run.
You could have lots and lots of these Classes.
index.php <php
include 'lib/Connection.php';
include 'lib/Email.php';
include 'lib/Class1.php';
include 'lib/Class2.php';
include 'lib/Class3.php';
include 'lib/Class4.php';
include 'lib/Class5.php';
include 'lib/Class6.php';
....
....
include 'lib/Class100.php';
$conn = new Connection();
// maybe some function in Connection class that returnsan array from the Database
$data = $conn->getSomeDataPlease();
var_dump($data);
echo "I just do echo.";
Now you see that the index.php is going to be very long. And it's the file is going to be big.
Not questioning if we have another file lets say a login.php.
We can make another dedicated file for these included files instead. And we will call it myautoload.php
- css
- js
- images
- lib
- myautoload.php
- index.php
myautoload.php <php
include 'lib/Connection.php';
include 'lib/Email.php';
include 'lib/Class1.php';
include 'lib/Class2.php';
include 'lib/Class3.php';
include 'lib/Class4.php';
include 'lib/Class5.php';
include 'lib/Class6.php';
//....
//....
include 'lib/Class100.php';
And we change index.php
index.php <php
include 'myautoload.php';
$conn = new Connection();
// maybe some function in Connection class that returnsan array from the Database
$data = $conn->getSomeDataPlease();
var_dump($data);
echo "I just do echo.";
Now the index.php is much cleaner.
Now the next thing we do is how to determine How to automatically load a PHP Class when needed?
We can use spl_autoload_register.
- fn [Required] - A function name in String or the function itself.
Returns boolean
Using this function we can "Auto Load" Classes in our files without even including all the Classes.
Here's how we are going to do it.
Firstly, we will replace the contents of our myautoload.php to use this function.
myautoload.php <php
spl_autoload_register(function( $class ){
$classfile = __DIR__ ."/lib/". $class . ".php";
if( file_exists($classfile) ){
include $classfile;
}
});
Now what this will do is only Call Classes that are being instantiated in a file.
So in our myautoload.php it is only including the "Connection" Class.
Think of it like this function scans the file for "Needed Classes" and only include those.
Let's do a bit more complicated Directory build.
- css
- js
- images
- lib
- extras
- Connection.php
- Email.php
- myautoload.php
- index.php
Now in this Directory example. Our "Autoload" will not work here since there are subfolders and the function needs to be dynamic in a way.
A solution to this would using a "Path Like" Class and using "Namespaces".
myautoload.php <php
spl_autoload_register(function( $class ){
$classfile = __DIR__ ."/". $class . ".php";
if( file_exists($classfile) ){
include $classfile;
}
});
index.php <php
include 'myautoload.php';
$conn = new lib\Connection();
$util = new lib\extras\Utility();
// maybe some function in Connection class that returnsan array from the Database
$data = $conn->getSomeDataPlease();
var_dump($data);
echo "I just do echo.";
An we add "Namespaces" to our classes.
Connection.php <php
namesapce lib; // basically saying "This file is inside the lib folder"
class Connection{
public function __construct(){
echo "I, Connection Class, was called!<br />";
}
public function getSomeDataPlease(){
// will return some Data from Database;
}
}
Email.php <php
namesapce lib; // basically saying "This file is inside the lib folder"
class Email{
public function __construct(){
echo "I, Email Class, was called!<br />";
}
}
Now you see index.php has this "Path Like" instantiation of the Classes.
We can then lessen these by "Aliasing" these "Path Like" Classes to be more easier.
myautoload.php <php
use lib\Connection as Connection;
use lib\Email as Email;
use lib\extras\Utility as Utility;
use lib\extras\Http as Http;
spl_autoload_register(function( $class ){
$classfile = __DIR__ ."/". $class . ".php";
if( file_exists($classfile) ){
include $classfile;
}
});
use lib\Connection as Connection; is saying "Everytime there is an instantiation of Connection, the file to be used is lib\Connection.php "
Now we only need to do is remove the "Path Like" Classes in the index.php file.
index.php <php
include 'myautoload.php';
$conn = new Connection();
$util = new Utility();
// maybe some function in Connection class that returnsan array from the Database
$data = $conn->getSomeDataPlease();
var_dump($data);
echo "I just do echo.";
Replies (0)
Reply