Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient PHP auto-loading and naming strategies

Like most web developers these days, I'm thoroughly enjoying the benefits of solid MVC architecture for web apps and sites. When doing MVC with PHP, autoloading obviously comes in extremely handy.

I've become a fan of spl_autoload_register over simply defining a single __autoload() function, as this is obviously more flexible if you are incorporating different base modules that each use their own autoloading. However, I've never felt great about the loading functions that I write. They involve a lot of string checking and directory scanning in order to look for possible classes to load.

For example, let's say I have an app that has a base path defined as PATH_APP, and a simple structure with directories named models, views and controllers. I often employ a naming structure whereby files are named IndexView.php and IndexController.php inside the appropriate directory, and models generally have no particular scheme by default. I might have a loader function for this structure like this that gets registered with spl_autoload_register:

public function MVCLoader($class) {     if (file_exists(PATH_APP.'/models/'.$class.'.php')) {         require_once(PATH_APP.'/models/'.$class.'.php');         return true;     }     else if (strpos($class,'View') !== false) {         if (file_exists(PATH_APP.'/views/'.$class.'.php')) {             require_once(PATH_APP.'/views/'.$class.'.php');             return true;         }     }     else if (strpos($class,'Controller') !== false) {         if (file_exists(PATH_APP.'/controllers/'.$class.'.php')) {             require_once(PATH_APP.'/controllers/'.$class.'.php');             return true;         }     }     return false; } 

If it's not found after that, I might have another function to scan sub-directories in the models directory. However, all the if/else-ing, string checking and directory scanning seems inefficient to me, and I'd like to improve it.

I'm very curious what file naming and autoloading strategies other developers might employ. I'm looking specifically for good techniques to employ for efficient autoloading, and not alternatives to autoloading.

like image 917
zombat Avatar asked Apr 27 '09 00:04

zombat


People also ask

How does PHP autoloader works?

The PHP Autoloader searches recursively in defined directories for class, trait and interface definitions. Without any further configuration the directory in which the requiring file resides will be used as default class path. File names don't need to obey any convention. All files are searched for class definitions.

What is class autoloading in PHP?

The spl_autoload_register() function registers any number of autoloaders, enabling for classes and interfaces to be automatically loaded if they are currently not defined. By registering autoloaders, PHP is given a last chance to load the class or interface before it fails with an error.

What is the relation of Namespacing and autoloading PHP files?

Basically it says: "inside this directory, all namespaces are represented by sub directories and classes are <ClassName>. php files." Autoloading is PHP's way to automatically find classes and their corresponding files without having to require all of them manually.

What is magic functions and auto loading in PHP?

In PHP __autoload() is a magic method, means it gets called automatically when you try create an object of the class and if the PHP engine doesn't find the class in the script it'll try to call __autoload() magic method. You can implement it as given below example: function __autoload($ClassName) { include($ClassName.


1 Answers

This is what I have been using in all of my projects (lifted straight from the source of the last one):

public static function loadClass($class) {     $files = array(         $class . '.php',         str_replace('_', '/', $class) . '.php',     );     foreach (explode(PATH_SEPARATOR, ini_get('include_path')) as $base_path)     {         foreach ($files as $file)         {             $path = "$base_path/$file";             if (file_exists($path) && is_readable($path))             {                 include_once $path;                 return;             }         }     } } 

If I look for SomeClass_SeperatedWith_Underscores it will look for SomeClass_SeperatedWith_Underscores.php followed by SomeClass/SeperatedWith/Underscores.php rooted at each directory in the current include path.

EDIT: I just wanted to put out there that I use this for efficiency in development, and not necessarily processing time. If you have PEAR on your path then with this you can just use the classes and don't have to include them when you need them.

I tend to keep my classes in a hierarchy of directories, with underscores breaking up namespaces... This code lets me keep the file structure nice and tidy if I want, or to inject a quick class file without nested directories if I want (for adding a single class or two to a library that it is defendant on, but not part of the project I am currently working on.)

like image 173
Mike Boers Avatar answered Sep 23 '22 10:09

Mike Boers