Efficient PHP Auto-Loading and Naming Strategies

Efficient PHP auto-loading and naming strategies

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.)

Force autoloading of a class without having the name

Well, if the autoload functions were added to spl_autoload_register() you should look at spl_autoload_functions().


Alternatively you could force the invocation of all registered autoloaders with spl_autoload_call().

Should I prefer require instead of require_once inside spl_autoload_register() callback

I've created a primitive benchmark.

I wasn't be able to see significant difference betwen this two approaches (require vs require_once). Maybe problem in "benchmark" itself, feel free to send PR to fix it :)

Most visible changes in execution time had took place when some other process has used disk while test running.

Both approaches had shown ~0.062 for 300 classes on 7200rpm HDD.

> php -v
PHP 7.2.1 (cli) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2017 Zend Technologies

file traversal type auto loading in codeigniter

CodeIgniter doesn't have PHP Autoloading build in, the only way to add it is by writing your own autoloader. I wrote a very extensive one a while back for CI: https://bitbucket.org/jschreuder/augmentedci/src/554e3d956a15/application/config/autoloader.php - though I'd probably do some things different now. If you'd search the forums you'd probably find some options as well.

One of the things you will run into is that CI uses the word "autoload" for something that should be "always-load", or maybe "autoload on each request". What you want is "autoload on demand" for classes, but that'll be a foreign concept to many CI users.

spl_autoload_register instantiate classes

That doesn't make any sense since autoload is called whenever you instantiate an object, and not for instantiating new objects.

Best practice to store a very small of data for extremely fast and efficient retrieval in php?

I decided to use an associated array within a config file.

My coord-config.php config file:

$coordinates = array( 

"P001" => array ("design_width" => 375,"dx" => 320,"dy" => 230,),
"P002" => array ("design_width" => 35,"dx" => 35,"dy" => 35,)

);

How I used in my image script:

require "/var/www/html/build/coord-config.php";

$design_width = $coordinates[$product]['design_width'];
$dx = $coordinates[$product]['dx'];
$dy = $coordinates[$product]['dy'];

What about file-path-in-class-name convention?

Then you haven't been looking. PEAR uses this approach. So does Zend Framework. It's a rather established standard in the PHP world. I don't know when I first encountered it, but I've personally used it and liked it for several years, both as part of my own framework and as part of publicly available frameworks such as ZF.



Related Topics



Leave a reply



Submit