note 92253 deleted from function.spl-autoload-register by thiago

From: Date: Thu, 16 Jul 2009 12:19:34 +0000
Subject: note 92253 deleted from function.spl-autoload-register by thiago
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-158365@lists.php.net to get a copy of this message
Note Submitter: rdheijer ---- My opinion: The autoloader is to be used with a slight touch of vigilance, since it can be used as well as misused. In my opinion, the autoloader is most useful when using naming conventions. For example a class called App_Controllers_Front, would load the file /app/controller/front.php to be able to make an instance of that class. Then, will within the autoloader directive and after including the file, check if the class exists within the current scope. Namespaces (php 5.3), however, may provide a whole new way of adressing the autoloader, but I haven't tried that yet. Back on track: Using naming conventions, your autoloader does not need to scan directories, but knows exactly where to look. If it isn't there, it should not be loaded. If you would scan subfolders, in rare cases the wrong class may be loaded (take into consideration david dot thalmann at gmail dot com's methodology). You can of course also use the spl_autoload_register() directive, but __autoload() is a lot easier because you can just create that function and it handles all calls. My own method is to create a class that is called by the __autoload() function, since classes provide a more structured way of the steps it is supposed to follow. Another benefit of a class is that you can step out of a method at any time just by calling return, for which you would have to put a lot of if/else blocks in your code if you approach it more script-wise, not to mention other excess code like extra checks. I could use the spl_autoload_register (which would provide the exact same functionality), but I don't. __autoload and a class is just as easy. --- Some comments... -- At Chris Corbyn: I'd recommend using a singleton in stead of a class that consists of static methods and vars. IMHO static methods and vars are not meant to be used in such a way. Just a remark, but your last method in the class ServiceLocator is not a static, thus generating errors if you would call it statically. -- At sandrejev at gmail dot com: I understand it works, but even then: what is your goal? It doesn't make sense to me to create a class with the class name and then still throw an error... Like carrying water to the sea. I would throw the exception immediately or create my own error feedback and call exit(), since the script does not need to run any further.

« previous php.notes (#158365) next »