note 72140 deleted from language.oop5.autoload by crell

From: Date: Mon, 04 Apr 2022 16:06:05 +0000
Subject: note 72140 deleted from language.oop5.autoload by crell
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-218013@lists.php.net to get a copy of this message
Note Submitter: Rico ---- This autoload function searches for the class Location before requiring it. So there's no need of putting the classes all in one folder. Requirements: - the subfolders must be at least 3 letters long - the filenames must be in the form CLASSNAME.class.php Note: - in this example the main class folder is 'lib' define('ROOT_DIR', dirname(__FILE__).'/'); function __autoload($className) { $folder=classFolder($className); if($folder) require_once($folder.'/'.$className.'.class.php'); } function classFolder($className,$folder='lib') { $dir=dir(ROOT_DIR.$folder); if($folder=='lib' && file_exists(ROOT_DIR.$folder.'/'.$className.'.class.php')) return $folder; else { while (false!==($entry=$dir->read())) { $checkFolder=$folder.'/'.$entry; if(strlen($entry)>2) { if(is_dir(ROOT_DIR.$checkFolder)) { if(file_exists(ROOT_DIR.$checkFolder.'/'.$className.'.class.php')) return $checkFolder; else { $subFolder=classFolder($className,$checkFolder); if($subFolder) return $subFolder; } } } } } $dir->close(); return 0; }

« previous php.notes (#218013) next »