Re: __autoload alternative

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Nathan Nobbe wrote:
On Mon, Sep 22, 2008 at 2:54 AM, Shelley <myphplist@xxxxxxxxx> wrote:

Hi 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;
  } else
    return true;
}

-nathan


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

Nathan

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux