note 63943 added to language.oop5.autoload

From: Date: Tue, 04 Apr 2006 14:15:55 +0000
Subject: note 63943 added to language.oop5.autoload
Groups: php.notes 
Request: Send a blank email to php-notes+get-107292@lists.php.net to get a copy of this message
Here's some code for a rules based autoloader I recently built and thought may be of use to someone else. The main rule here is for loading classes from a CoreLib/ directory. A class might be named something like 'CoreState' which would require the file 'CoreLib/core.state.class.php'. Simply add your own rules (PCRE RegExps) to the array and away you go. The only missing functionality I can think of is for using the same pattern for looking in multiple directories for the source file. Because I'm using the pattern as the key for the rules hash, reusing a pattern will overwrite any previous rules with that pattern. function __autoload($class_name) { $rules = array( "/Core(.+)/e" => "'CoreLib/core.'.strtolower('\\1').'.class.php'", "/Smarty/" => "CoreLib/3p/Smarty/Smarty.class.php" ); foreach ($rules as $patt => $repl) { $count = 0; $class_file = preg_replace($patt,$repl,$class_name,-1,$count); if ($count > 0 && file_exists($class_file)) { require_once($class_file); return true; } } // place some error trapping here if you think it's important } ---- Server IP: 203.220.0.234 Probable Submitter: 202.7.176.134 (proxied: 220.245.156.199) ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.autoload.php Edit -- http://master.php.net/note/edit/63943 Del: integrated -- http://master.php.net/note/delete/63943/integrated Del: useless -- http://master.php.net/note/delete/63943/useless Del: bad code -- http://master.php.net/note/delete/63943/bad+code Del: spam -- http://master.php.net/note/delete/63943/spam Del: non-english -- http://master.php.net/note/delete/63943/non-english Del: in docs -- http://master.php.net/note/delete/63943/in+docs Del: other reasons-- http://master.php.net/note/delete/63943 Reject -- http://master.php.net/note/reject/63943 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#107292) next »