ä¸‡èƒ½çš„ç±»æ–‡ä»¶åŠ è½½å™¨ ====================== :doc:`Phalcon\\Loader <../api/Phalcon_Loader>` 这个组件å…è®¸ä½ æ ¹æ®é¢„å®šä¹‰è§„åˆ™è‡ªåŠ¨åŠ è½½é¡¹ç›®ä¸çš„类文件。 æ¤ç»„件采用Cè¯è¨€ç¼–写,在读å–和解æžPHP文件方é¢ï¼Œä½¿ç”¨äº†æœ€ä½Žçš„æ€§èƒ½å¼€é”€ã€‚ æ¤ç»„件功能是基于PHP自身的 `autoloading classes`_ 实现的。如果在任何代ç 部分未找到类,那么它将å°è¯•使用特殊的处ç†åŠ è½½å®ƒ,:doc:`Phalcon\\Loader <../api/Phalcon_Loader>` 就是用于处ç†è¿™ç§ä»»åŠ¡çš„ã€‚åŠ è½½ç±»æ–‡ä»¶é‡‡ç”¨æŒ‰éœ€åŠ è½½çš„æ–¹å¼ï¼Œåªæœ‰éœ€è¦æŸä¸ªç±»æ–‡ä»¶æ—¶ï¼Œæ‰ä¼šè¿›è¡ŒåŠ è½½åŠæ–‡ä»¶è¯»å–ã€‚è¿™ç§æŠ€æœ¯è¢«ç§°ä¸ºå»¶æ—¶åˆå§‹åŒ–( `lazy initialization`_ )。 使用æ¤ç»„ä»¶ï¼Œä½ å¯ä»¥ä»Žå…¶ä»–项目或vendors(ä¸çŸ¥é“是什么,应该指的是其他æœåС噍)åŠ è½½æ–‡ä»¶ï¼Œautoloader 采用 `PSR-0 <https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md>`_ æ ‡å‡†ã€‚ :doc:`Phalcon\\Loader <../api/Phalcon_Loader>` æä¾›å››ç§æ–¹å¼è‡ªåŠ¨åŠ è½½ç±»æ–‡ä»¶ï¼Œä½ å¯ä»¥ä¸€æ¬¡åªä½¿ç”¨å…¶ä¸ä¸€ä¸ªï¼Œæˆ–者混åˆä½¿ç”¨ã€‚ Registering Namespaces ---------------------- å¦‚æžœä½ çš„ä»£ç 使用了命å空间或外部库文件,å¯ä»¥ä½¿ç”¨registerNamespaces(),它æä¾›äº†ä¸€ç§è‡ªåŠ¨åŠ è½½æœºåˆ¶ï¼Œå¯ä»¥é€šè¿‡ä¼ é€’ä¸€ä¸ªå…³è”æ•°ç»„,key是命å空间å‰è¾ï¼Œvalueæ˜¯ç±»æ–‡ä»¶å˜æ”¾çš„ç›®å½•ã€‚å½“åŠ è½½å¹¶è¯•å›¾æŸ¥æ‰¾ç±»æ–‡ä»¶æ—¶ï¼Œåˆ†éš”ç¬¦è¢«æ›¿æ¢æˆç›®å½•分隔符,以便æ£ç¡®åŠ è½½ç±»æ–‡ä»¶ã€‚è¿˜æœ‰ä¸€ç‚¹è¯·æ³¨æ„,value末尾一定è¦ä»¥"/"结尾。 .. code-block:: php <?php // Creates the autoloader $loader = new \Phalcon\Loader(); //Register some namespaces $loader->registerNamespaces( array( "Example\Base" => "vendor/example/base/", "Example\Adapter" => "vendor/example/adapter/", "Example" => "vendor/example/", ) ); // register autoloader $loader->register(); // The required class will automatically include the // file vendor/example/adapter/Some.php $some = new Example\Adapter\Some(); Registering Prefixes ---------------------- è¿™ç§ç–ç•¥éžå¸¸ç±»ä¼¼ä¸Šé¢è®²åˆ°çš„命åç©ºé—´çš„åŠ è½½æœºåˆ¶ã€‚å®ƒä¹Ÿéœ€è¦ä¸€ä¸ªå…³è”数组,key是å‰è¾ï¼Œvalueæ˜¯ç±»æ‰€åœ¨çš„ç›®å½•ã€‚åŠ è½½çš„æ—¶å€™ï¼Œå‘½å空间分隔符和下划线"_"å°†è¦è¢«æ›¿æ¢ä¸ºç›®å½•分隔符。还是请注æ„,value的结尾一定è¦ä»¥"/"作为结æŸç¬¦ã€‚ .. code-block:: php <?php // Creates the autoloader $loader = new \Phalcon\Loader(); //Register some prefixes $loader->registerPrefixes( array( "Example_Base" => "vendor/example/base/", "Example_Adapter" => "vendor/example/adapter/", "Example_" => "vendor/example/", ) ); // register autoloader $loader->register(); // The required class will automatically include the // file vendor/example/adapter/Some.php $some = new Example_Adapter_Some(); Registering Directories ----------------------- ç¬¬ä¸‰ç§æ–¹å¼æ˜¯æ³¨å†Œç›®å½•ï¼Œåœ¨æ³¨å†Œçš„ç›®å½•ä¸æ‰¾åˆ°ç±»æ–‡ä»¶ã€‚在性能方é¢ï¼Œä¸å»ºè®®ä½¿ç”¨æ¤ç§æ–¹å¼ï¼Œå› 为Phalconå°†å¯¹æ³¨å†Œçš„æ‰€æœ‰ç›®å½•åŠæ–‡ä»¶è¿›è¡ŒæŸ¥æ‰¾ï¼Œç›´æŽ¥æŸ¥æ‰¾å…·æœ‰ç›¸åŒåç§°çš„ç±»æ–‡ä»¶ã€‚æ³¨å†Œç›®å½•æ—¶çš„é¡ºåºæ˜¯éžå¸¸é‡è¦çš„。请注æ„,结尾以"/"结æŸã€‚ .. code-block:: php <?php // Creates the autoloader $loader = new \Phalcon\Loader(); // Register some directories $loader->registerDirs( array( "library/MyComponent/", "library/OtherComponent/Other/", "vendor/example/adapters/", "vendor/example/" ) ); // register autoloader $loader->register(); // The required class will automatically include the file from // the first directory where it has been located // i.e. library/OtherComponent/Other/Some.php $some = new Some(); Registering Classes ------------------- 最åŽä¸€ç§æ–¹å¼æ˜¯æ³¨å†Œç±»çš„å称和路径。这ç§åŠ è½½æ–¹é¢æ˜¯æœ€å¿«çš„一ç§åŠ è½½æ–¹å¼ã€‚然而,éšç€åº”用程åºçš„å¢žé•¿ï¼Œæ›´å¤šçš„ç±»åŠæ–‡ä»¶éœ€è¦åŠ è½½ï¼Œè¿™å°†ä½¿ç»´æŠ¤å·¥ä½œå˜å¾—éžå¸¸éº»çƒ¦ï¼Œå› 为ä¸å¤ªå»ºè®®ä½¿ç”¨ã€‚ .. code-block:: php <?php // Creates the autoloader $loader = new \Phalcon\Loader(); // Register some classes $loader->registerClasses( array( "Some" => "library/OtherComponent/Other/Some.php", "Example\Base" => "vendor/example/adapters/Example/BaseClass.php", ) ); // register autoloader $loader->register(); // Requiring a class will automatically include the file it references // in the associative array // i.e. library/OtherComponent/Other/Some.php $some = new Some(); å…¶ä»–æ‰©å±•åæ–‡ä»¶çš„åŠ è½½ -------------------------- ä¸€äº›è‡ªåŠ¨åŠ è½½ç–略,如"prefixes","namespaces",或"directories"éƒ½ä¼šè‡ªåŠ¨åŠ è½½æ‰©å±•å为".php"çš„æ–‡ä»¶ã€‚å¦‚æžœä½ æƒ³è‡ªåŠ¨åŠ è½½å…¶ä»–æ‰©å±•ç±»åž‹çš„æ–‡ä»¶æ—¶ï¼Œä½ å¯ä»¥ä½¿ç”¨"setExtensions"方法。示例如下: .. code-block:: php <?php // Creates the autoloader $loader = new \Phalcon\Loader(); //Set file extensions to check $loader->setExtensions(array("php", "inc", "phb")); Modifying current strategies ---------------------------- 通过下é¢çš„æ–¹å¼å¯ä»¥æŠŠéœ€è¦åŽæ¥åŠ è½½çš„å…¶ä»–æ–‡ä»¶åˆå¹¶åˆ°ä¸Šè¿°åŠ è½½ç–ç•¥ä¸ï¼š .. code-block:: php <?php // Adding more directories $loader->registerDirs( array( "../app/library/" "../app/plugins/" ), true ); é€šè¿‡ä¼ é€’ç¬¬äºŒä¸ªå‚æ•°"true",å¯ä»¥è®©æ–°çš„目录或类文件åˆå¹¶åˆ°ä¸Šè¿°å››ç§åŠ è½½ç–ç•¥ä¸ã€‚ Autoloading Events ------------------ 在下é¢çš„例åä¸ï¼ŒEventsManagerä¸Žç±»åŠ è½½å™¨ååŒå·¥ä½œï¼Œä½¿æˆ‘们能够获得æ“作æµç¨‹çš„调试信æ¯ï¼š .. code-block:: php <?php $eventsManager = new \Phalcon\Events\Manager(); $loader = new \Phalcon\Loader(); $loader->registerNamespaces(array( 'Example\\Base' => 'vendor/example/base/', 'Example\\Adapter' => 'vendor/example/adapter/', 'Example' => 'vendor/example/' )); //Listen all the loader events $eventsManager->attach('loader', function($event, $loader) { if ($event->getType() == 'beforeCheckPath') { echo $loader->getCheckedPath(); } }); $loader->setEventsManager($eventsManager); $loader->register(); 当事件返回布尔值false时,å¯ä»¥åœæ¢æ¿€æ´»çš„æ“ä½œã€‚æ”¯æŒä»¥ä¸‹ä¸€äº›äº‹ä»¶ï¼š +------------------+---------------------------------------------------------------------------------------------------------------------+---------------------+ | Event Name | Triggered | Can stop operation? | +==================+=====================================================================================================================+=====================+ | beforeCheckClass | Triggered before start the autoloading process | Yes | +------------------+---------------------------------------------------------------------------------------------------------------------+---------------------+ | pathFound | Triggered when the loader locate a class | No | +------------------+---------------------------------------------------------------------------------------------------------------------+---------------------+ | afterCheckClass | Triggered after finish the autoloading process. If this event is launched the autoloader didn't find the class file | No | +------------------+-----------------------------------------------------------+---------------------------------------------------------+---------------------+ .. _autoloading classes: http://www.php.net/manual/en/language.oop5.autoload.php .. _lazy initialization: http://en.wikipedia.org/wiki/Lazy_initialization