Re: __autoload alternative
| From: | Nathan Rixham | Date: | Mon, 22 Sep 2008 15:59:28 +0000 |
| Subject: | Re: __autoload alternative | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-280700@lists.php.net to get a copy of this message | ||
Nathan Nobbe wrote:
On Mon, Sep 22, 2008 at 2:54 AM, Shelley <myphplist@gmail.com> wrote:that's what I do too.. personally I use _ (underscores) in PHP like . (periods) in Java to map objects to folders/files.. function __autoload($class) { if(file_exists( SOME_BASE_DIR . parse_class_name($class) )) { include_once SOME_BASE_DIR . parse_class_name($class); } else { #your error here } } function parse_class_name( $classname ) { return strtolower( str_replace('_', DIRECTORY_SEPARATOR, $classname) . '.php' ); } so: <?php $http_handler = new http_handler; ?> would autoload SOME_BASE_DIR/http/handler.php whilst <?php $atom_parser = new atom_parser; ?> would autoload SOME_BASE_DIR/atom/parser.php keeps my files all nice and organised and saves me writing map's :) NathanHi all, Is there any way to auto load a class without using __autoload() function? As I want to load some classes under different paths, and that caused redeclare of __autoload function.a lot of people / frameworks, etc. define their own class loading mechanism in userspace. heres a simple example, function _autoload($class) { include(STD_CLASS_PATH . "/$class.php"); if(!class_exists($class)) {trigger_error("unable to load class $class.", E_USER_NOTICE); return false;} elsereturn true;} -nathan