PHP5 Autoload classes

This is really handy for medium to large size projects. You don’t have to remember to put a set of require_once functions in every file.  I usually put this in a config file that’s loaded early and on every page.

<?php
function __autoload($class_name) {
   require_once $class_name . ‘.php’;
}

$obj  = new MyClass1();
$obj2 = new MyClass2();
?>

See http://us2.php.net/manual/en/language.oop5.autoload.php for more information.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

* Copy this password:

* Type or paste password here:

16,278 Spam Comments Blocked so far by Spam Free Wordpress

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Spam Protection by WP-SpamFree