PHP 8.5.0 Alpha 2 available for testing

Voting

: min(one, zero)?
(Example: nine)

The Note You're Voting On

a dot schaffhirt at sedna-soft dot de
15 years ago
Good news for PHP 5.3 users with namespaced classes:

When you create a subfolder structure matching the namespaces of the containing classes, you will never even have to define an autoloader.

<?php
spl_autoload_extensions
(".php"); // comma-separated list
spl_autoload_register();
?>

It is recommended to use only one extension for all classes. PHP (more exactly spl_autoload) does the rest for you and is even quicker than a semantically equal self-defined autoload function like this one:

<?php
function my_autoload ($pClassName) {
include(
__DIR__ . "/" . $pClassName . ".php");
}
spl_autoload_register("my_autoload");
?>

I compared them with the following setting: There are 10 folders, each having 10 subfolders, each having 10 subfolders, each containing 10 classes.

To load and instantiate these 1000 classes (parameterless no-action constructor), the user-definded autoload function approach took 50ms longer in average than the spl_autoload function in a series of 10 command-line calls for each approach.

I made this benchmark to ensure that I don't recommend something that could be called "nice, but slow" later.

Best regards,

<< Back to user notes page

To Top