using autoload with PEAR


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

__autoload is often used to load class files from a specific user-defined directory. by simply adding the user-defined directory to the include_path, however, autoload can be used to load user class files as well as PEAR class files. Assuming the PEAR naming convention is adhered to for the user defined classes.


Copy this code and paste it in your HTML
  1. function __autoload($classname)
  2. {
  3. // put the path to your class files here
  4. $my_path = "/var/www/mydomain.com/lib";
  5.  
  6. // tell PHP to scan the default include paht AND your include path
  7. set_include_path(get_include_path() . PATH_SEPARATOR . $my_path);
  8.  
  9. // name your classes and filenames with underscores, i.e., Net_Whois stored in Net_Whois.php
  10. $classfile = str_replace("_", "/", $classname) . ".php";
  11.  
  12. include_once($classfile);
  13. }
  14.  
  15. /**
  16.  * EXAMPLE:
  17.  * create one of your objects, saved in /var/www/mydomain.com/lib/Project/Database.php
  18.  */
  19. $db = new Project_Database();
  20.  
  21.  
  22. /**
  23.  * EXAMPLE:
  24.  * create a PEAR object, saved in /usr/local/lib/php/File.php
  25.  */
  26.  
  27. $f = new File();

URL: http://www.drewcking.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.