Jump to content


Photo
* - - - - 1 votes

[PHP 5] Auto Loading classes


  • Please log in to reply
No replies to this topic

#1 MalDON

MalDON

    Young Padawan

  • Members
  • Pip
  • 127 posts
  • Location:Southern California
  • Interests:DeviantART, PHP, Techno, Empire Earth 2, Halo 2

Posted 08 May 2005 - 07:08 PM

At time sincluding a bunch of class files can become very messy, and sometimes those included pages are never used. Good thing for us that a new function available only for PHP 5 is the autoload class.

<?php
  
  function __autoload($class) {
    include("inc/$class.php");
  }

  $myclass = new MyClass();

?>

In the above example, PHP will check is there are any missing classes. If there is then the __autoload class will include the file when needed. I have found that when working with much larger projects, in which case use a config file, it is best to have the __aotoload function somewhere in the config file. This will ensure that all pages will have access to all classes.

Oh, by the way. I must say again. This will not work in PHP 4 or below.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users