Autoloading Objects


/ Published in: PHP
Save to your folder(s)

http://us.php.net/manual/en/language.oop5.autoload.php#82614


Copy this code and paste it in your HTML
  1. <?php
  2. function __autoload($class_name)
  3. {
  4. //class directories
  5. $directorys = array(
  6. 'classes/',
  7. 'classes/otherclasses/',
  8. 'classes2/',
  9. 'module1/classes/'
  10. );
  11.  
  12. //for each directory
  13. foreach($directorys as $directory)
  14. {
  15. //see if the file exsists
  16. if(file_exists($directory.$class_name . '.php'))
  17. {
  18. require_once($directory.$class_name . '.php');
  19. //only require the class once, so quit after to save effort (if you got more, then name them something else
  20. return;
  21. }
  22. }
  23. }
  24. ?>

URL: http://us3.php.net/__autoload

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.