======[PHP] InlcudeClasses====== Diese Klasse ist meine Alternative zu __autoload(). __autoload() wird bei grösseren Projekten schnell langsam und unübersichtlich. Die Pfade werden ab dem root in der Datei 'include.properties' hinterlegt. Wenn eine Datei bereits mit include_once() geladen wurde, dann wird dies gespeichert und bei erneutem laden nicht nochmal geladen. Achtung. Diese Klasse ist eine Vererbung von [[:php:libraries:propertyreader]]. Diese wird also auch verwendet. $propetyFile Name der Propertydatei */ public static function singleton($propetyFile = self::PROPERTIES_DEFAULT_PATH){ if (!isset(self::$instance)) { $c = __CLASS__; self::$instance = new $c; } return self::$instance; } /** * include_once auf ein Propertiy * @param $className Name des Property * @param $propetyFile Name der Propertydatei */ public static function import($className, $propetyFile = self::PROPERTIES_DEFAULT_PATH){ try { self::singleton($propetyFile)->includeByKey($className); } catch (Exception $e){ echo ($e); } } /** * @var Array array of included filepaths * @access private */ private $cach = array(); /** * @access public */ public function __construct($propetyFile = self::PROPERTIES_DEFAULT_PATH){ parent::__construct($propetyFile); } /** * @access public * @param String key * @retrun Boolean * @throw Exception */ public function includeByKey($key){ $path = $this->$key; $retVal = false; /** check if the file is allreadx included */ if (in_array($path, $this->cach)){ /** check if the file exist */ } elseif (file_exists($path)){ /** include the file */ include_once($path); /** add the path to the cach */ $this->cach[] = $path; $retVal = true; /** else create error */ } else { throw new Exception(__METHOD__." cant include the file {$key} => {$path}"); } return $retVal; } } ?> Und hier ein Beispiel eines entsprechenden Proerty-Files ## # @Copyright mpl by ERB software # @cauthor stefan.erb@erb-software.com ## ## # Wird von der Library IncludeClasses verwendet. ## #Functions filterInputDefaults = lib/functions/filterInputDefaults.php ifnull = lib/functions/ifnull.php buildPath = lib/functions/buildPath.php objectToArray = lib/functions/objectToArray.php ifNotKeyExists = lib/functions/ifNotKeyExists.php array_extract_sub_item = lib/functions/array_extract_sub_item.php #Libraries Libs = lib/Libs.php Database = lib/db/mysql/include.php Smarty = lib/Smarty/libs/Smarty.class.php #Exceptions UserException = app/exceptions/UserException.php FieldException = app/exceptions/FieldException.php UserExceptions = app/exceptions/UserExceptions.php FieldExceptions = app/exceptions/FieldExceptions.php #Application DropDownListDataFactory = app/DropDownListDataFactory.php FileInfo = app/FileInfo.php Checker = app/Checker.php SqlHelper = app/SqlHelper.php #Application.Upload Uploader = app/upload/Uploader.php UploadProperties = app/upload/UploadProperties.php #Application.Upload.FieldDefs IUploadField = app/upload/fieldDefs/IUploadField.php UploadField = app/upload/fieldDefs/UploadField.php UploadFieldFile = app/upload/fieldDefs/UploadFieldFile.php #Action UploadUkatDDList = webComponent/action/classes/UploadUkatDDList.php #Ajax AjaxUploadUkatDDList = webComponent/ajax/ajaxUploadUkatDDList.php AjaxUploadFile = webComponent/ajax/ajaxUploadFile.php Am Anfang jedes Scriptes liste ich nun einfach alle Dinge auf die ich verwende und kümmere mich nicht mehr darum ob das Ding schon geladen ist und was der genaue Pfad ist. Im gegensatz zu autoload können so auch Nicht-Klassenscripte so geladen werden.