note 92514 added to function.spl-autoload-register

From: Date: Tue, 28 Jul 2009 02:05:03 +0000
Subject: note 92514 added to function.spl-autoload-register
Groups: php.notes 
Request: Send a blank email to php-notes+get-158803@lists.php.net to get a copy of this message
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, ---- Server IP: 217.160.72.57 Probable Submitter: 77.179.217.157 ---- Manual Page -- http://www.php.net/manual/en/function.spl-autoload-register.php Edit -- https://master.php.net/note/edit/92514 Del: integrated -- https://master.php.net/note/delete/92514/integrated Del: useless -- https://master.php.net/note/delete/92514/useless Del: bad code -- https://master.php.net/note/delete/92514/bad+code Del: spam -- https://master.php.net/note/delete/92514/spam Del: non-english -- https://master.php.net/note/delete/92514/non-english Del: in docs -- https://master.php.net/note/delete/92514/in+docs Del: other reasons-- https://master.php.net/note/delete/92514 Reject -- https://master.php.net/note/reject/92514 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#158803) next »