note 72831 deleted from language.oop5.autoload by crell

From: Date: Mon, 04 Apr 2022 16:12:13 +0000
Subject: note 72831 deleted from language.oop5.autoload by crell
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-218034@lists.php.net to get a copy of this message
Note Submitter: david dot thalmann at gmail dot com ---- Note to Ricos posting: A lot of useless Coding. However, I improved the code, so now it will be able to find any folders ("." and ".." will not being tested... oO) and search as deep as possible. Now it will find CLASS_DIR/foo/bar.class.php also like CLASS_DIR/foo/bar/baz/buz/fii/and/so/on/class.php Warning: This code will check ALL dirs who're "deeper" / "lower" than the class dir, so prevent deeply hidden files (or use just a few folders). Improved Version: <?php // change this, if this code isn't "higher" than ALL classfiles define("CLASS_DIR", dirname(__FILE__)); /** * autoload classes (no need to include them one by one) * * @uses classFolder() * @param $className string */ function __autoload($className) { $folder = classFolder($className); if($folder) require_once($folder.$className.".class.php"); } /** * search for folders and subfolders with classes * * @param $className string * @param $sub string[optional] * @return string */ function classFolder($className, $sub = "/") { $dir = dir(CLASS_DIR.$sub); if(file_exists(CLASS_DIR.$sub.$className.".class.php")) return CLASS_DIR.$sub; while(false !== ($folder = $dir->read())) { if($folder != "." && $folder != "..") { if(is_dir(CLASS_DIR.$sub.$folder)) { $subFolder = classFolder($className, $sub.$folder."/"); if($subFolder) return $subFolder; } } } $dir->close(); return false; } ?>

« previous php.notes (#218034) next »