When we work on a small project then we include all the related files in the main configuration file. But as the project grows then it becomes tedious to include individually. This process of addition happens by using include/require statement.
In the world of PHP, we have two main options to start a project. Either via core PHP or via PHP Framework.
If we planned to use PHP Framework (like CodeIgniter, Symfony, Laravel) then we do not need to worry about including files manually. There are framework based ways to add files OR there is a composer to add PHP dependencies and include all such dependencies by default for us.
But in case of core PHP, as we need to manually decide folder structure and their exact relation and use in the codebase. Hence in such a case, we need a concrete solution to manage all such files and make sure that there will be no more/additional changes required to handle file level complexities.
Let's first understand the basic example of how files were accessed in the traditional PHP code files.
-- example.php include("function/config.php"); include("function/database.class.php"); include("class/validation.class.php"); include("class/errors.class.php"); $objDb = new database(); $objV = new validation(); $objE = new errors(); $objDb->connect(); $objV->validate(); $objE->ErrorCheck();
In the traditional way of writing code, we have to first include a long list of files at the beginning because those are the files which are the root files for our project and depending on those files we will perform a different operation in the ongoing functionalities.
As mentioned above, this process becomes tedious as soon as our project starts to increase. Hence, in PHP 5 __autoload() magic function was introduced to solve this complexity.This function does not require to be included in the class file. This function can be accessed globally.
Let's take a basic example of __autoload() function-
-- index.php function __autoload($project_class) { $filename = __DIR__ . "/classes/$project_class.php"; include_once($filename); return; } $objClass = new className();
In the above example, we have included files using magic function __autoload(). It takes class name as a parameter and then this class is included for us. This looks usual, but what will happen when in the class directory we added new files. Do we need to include them again manually in the index.php file? The answer is NO. PHP's __autoload() function will automatically include that file in the project and depending on its use we can create its object (if required means if the included file is class file) on required location and that's it. we can start using it.
How __autoload() magic function works?
When we try to create an object of any class then autoload function gets called and that file gets included before an object of that file class is created.SIMPLE, right?
Above example is the basic example with the same directory files inclusion. What will happen if we want to include multiple folders files in the index.php file? is that possible using __autoload() function. And the answer is YES. But to make it possible we need to make some modification of the original code and then it will be ready to use.
Now, Let's rewrite above example in a new way, I mean using __autoload() magic method.
-- example.php function __autoload($project_classfile) { $folder_regular_file = "classes/{$project_classfile}.php"; if (is_file($folder_regular_file) && is_readable($folder_regular_file)){ include_once $folder_regular_file; return; } $folder_vendor_file = "vendor/{$project_classfile}.php"; if (is_file($folder_vendor_file) && is_readable($folder_vendor_file)){ include_once $folder_vendor_file; return; } } $objDb = new database(); $objV = new validation(); $objE = new errors(); $objDb->connect(); $objV->validate(); $objE->ErrorCheck();In the above, most of the code in the __autoload() function is duplicate because we are just changing the directory name where that file might be present. But this code is reluctant, we need a more optimized way to write this code. We can create an array where we can just add respective directory names and then in the for loop we can check that file.
Now, let's see how-
-- example.php function __autoload($project_classfile) { $directories = array( 'classes', 'vendor' ); foreach($directories as $path) { $file_path = "{$path}/{$project_classfile}.php"; if (is_file($file_path) && is_readable($file_path)){ include_once $file_path; } } } $objDb = new database(); $objV = new validation(); $objE = new errors(); $objDb->connect(); $objV->validate(); $objE->ErrorCheck();
What will happen in the above code?
When PHP tries to create an object of database class then __autoload() function will get called automatically and then it will check the existence of database class in the classes folder and if it is not present then it will look for the file in the vendor file. If that file is present there then it will include it. That's it.Next time when we want to include other directories files then in the $directories array we just need to add that directory name.
This is the basic example of how __autoload() works and how we can use them in the project.
This __autoload() magic functions are also used while working with namespaces. The functionality of how namespaces works are already covered in this post. Please check and if you have any queries/questions then please add them in the comment section.
No comments:
Post a Comment