Do you use __autoload in PHP5?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Do you use __autoload in PHP5?

Post by nielsene »

Been reading over the PHP4->5 changes again, and the __autoload magic function caught my eye. At first glance it looked like a wonderful solution to longer header files of inclues. However, the manual examples, at least, show a global function for the entire class space. Which basically would entail sticking all the classes into a single directory so it can find them... When you have lots of class that seems like enough of an annoyance to destroy its value.

Any one out there with experience using it?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

on a PHP 5 side note, I can see str_ireplace being very useful.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Nope, never used it, never will (to keep PHP4 backwards compatibility) but, you can make the function a bit more context sensitive that will let you put them in other directories... personally, I stick all my classes in one directory.
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

i wrote a class with static variables that could be set to directories from which include classes.
i was trying to do something similar to namespaces, but it wasn't quite the same.
it was something like:

Code: Select all

<?
class MOD {
  public static $dirs;  // array
  public static function import($dir) {
    // add the dir to static $dirs array
  }
}
function __autoload ($class_name) {
  foreach (MOD::$dirs as $dir) {
     // include file if it exists on that directory
  }
}
      

MOD::import('Dir.SubDir');

$p = new Page; // class in Dir/SubDir/Page.php

?>
Post Reply