rfc:splclassloader

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
rfc:splclassloader [2011/11/08 14:40]
guilhermeblanco Added updated SplClassLoader implementation
rfc:splclassloader [2017/09/22 13:28] (current)
Line 3: Line 3:
   * Date: 2010-03-22   * Date: 2010-03-22
   * Author: Guilherme Blanco <guilhermeblanco@hotmail.com>   * Author: Guilherme Blanco <guilhermeblanco@hotmail.com>
-  * Status: Voting at https://wiki.php.net/rfc/splclassloader/vote +  * Status: Declined 
 +  * Voting results at https://wiki.php.net/rfc/splclassloader/vote 
   * First Published at: http://wiki.php.net/rfc/splclassloader   * First Published at: http://wiki.php.net/rfc/splclassloader
  
Line 77: Line 78:
          
     /**     /**
-     Creates a new Autoloader instance.+     Define the autoloader work mode.
      *      *
      * @param integer $mode Autoloader work mode.      * @param integer $mode Autoloader work mode.
      */      */
-    public function __construct($mode = \SplAutoloader::MODE_NORMAL);+    public function setMode($mode);
          
     /**     /**
Line 145: Line 146:
  
 <code php> <code php>
-$classLoader = new \SplClassLoader(\SplClassLoader::MODE_SILENT);+$classLoader = new \SplClassLoader(); 
 +$classLoader->setMode(\SplClassLoader::MODE_SILENT);
 $classLoader->setIncludePathLookup(true); $classLoader->setIncludePathLookup(true);
 $classLoader->add('PEAR'); $classLoader->add('PEAR');
Line 154: Line 156:
  
 <code php> <code php>
-$classLoader = new \SplClassLoader(\SplClassLoader::MODE_NORMAL | \SplClassLoader::MODE_DEBUG);+$classLoader = new \SplClassLoader(); 
 +$classLoader->setMode(\SplClassLoader::MODE_NORMAL | \SplClassLoader::MODE_DEBUG);
 $classLoader->add('Symfony', '/path/to/symfony'); $classLoader->add('Symfony', '/path/to/symfony');
 $classLoader->add('Zend', '/path/to/zf'); $classLoader->add('Zend', '/path/to/zf');
Line 195: Line 198:
  *  *
  * Example usage:  * Example usage:
 + *
 +     $classLoader = new \SplClassLoader();
  *  *
      // Configure the SplClassLoader to act normally or silently      // Configure the SplClassLoader to act normally or silently
-     $classLoader = new \SplClassLoader(\SplClassLoader::MODE_NORMAL);+     $classLoader->setMode(\SplClassLoader::MODE_NORMAL);
  *  *
      // Add a namespace of classes      // Add a namespace of classes
Line 246: Line 251:
      * @var integer      * @var integer
      */      */
-    private $mode;+    private $mode = self::MODE_NORMAL;
  
     /**     /**
-     Creates a new <tt>SplClassLoader</tt> that loads classes of the specified namespace. +     {@inheritdoc}
-     * +
-     @param integer $mode Autoloader work mode.+
      */      */
-    public function __construct($mode = self::MODE_NORMAL)+    public function setMode($mode)
     {     {
     if ($mode & self::MODE_SILENT && $mode & self::MODE_NORMAL) {     if ($mode & self::MODE_SILENT && $mode & self::MODE_NORMAL) {
-    throw new \InvalidArgumentException( +        throw new \InvalidArgumentException( 
-         sprintf('Cannot have %s working normally and silently at the same time!', __CLASS__) +             sprintf('Cannot have %s working normally and silently at the same time!', __CLASS__) 
-     );+         );
     }     }
          
Line 336: Line 339:
                  
         switch (true) {         switch (true) {
-        case ($this->mode & self::MODE_SILENT): +            case ($this->mode & self::MODE_SILENT): 
-             if ($resourceAbsolutePath !== false) { +                if ($resourceAbsolutePath !== false) { 
-             require $resourceAbsolutePath; +                    require $resourceAbsolutePath; 
-             +                
-             break;+                break;
                  
             case ($this->mode & self::MODE_NORMAL):             case ($this->mode & self::MODE_NORMAL):
Line 349: Line 352:
                  
         if ($this->mode & self::MODE_DEBUG && ! $this->isResourceDeclared($resourceName)) {         if ($this->mode & self::MODE_DEBUG && ! $this->isResourceDeclared($resourceName)) {
-        throw new \RuntimeException( +            throw new \RuntimeException( 
-         sprintf('Autoloader expected resource "%s" to be declared in file "%s".', $resourceName, $resourceAbsolutePath) +                sprintf('Autoloader expected resource "%s" to be declared in file "%s".', $resourceName, $resourceAbsolutePath) 
-         );+            );
         }         }
     }     }
Line 363: Line 366:
      */      */
     private function getResourceAbsolutePath($resourceName)     private function getResourceAbsolutePath($resourceName)
 +    {
         $resourceRelativePath = $this->getResourceRelativePath($resourceName);         $resourceRelativePath = $this->getResourceRelativePath($resourceName);
                  
Line 380: Line 384:
                  
         if ($this->includePathLookup && ($resourceAbsolutePath = stream_resolve_include_path($resourceRelativePath)) !== false) {         if ($this->includePathLookup && ($resourceAbsolutePath = stream_resolve_include_path($resourceRelativePath)) !== false) {
-        return $resourceAbsolutePath;+            return $resourceAbsolutePath;
         }         }
                  
Line 424: Line 428:
 } }
 </code> </code>
 +
 +If any interested wants to customize the public methods, like caching through APC to reduce I/O, it should be possible to extend SplClassLoader and overwrite the public methods.
  
 ===== Proposal and Patch ===== ===== Proposal and Patch =====
Line 437: Line 443:
 ===== Changelog ===== ===== Changelog =====
  
 +  * 2011-11-09 Christian Kaps: Update examples to use the new interface(setMode)
 +  * 2011-11-08 Guilherme Blanco: Removed constructor prototype and created setMode.
   * 2011-11-08 Guilherme Blanco: Updated SplClassLoader implementation.   * 2011-11-08 Guilherme Blanco: Updated SplClassLoader implementation.
   * 2011-11-07 Guilherme Blanco: Expanded extended rules.   * 2011-11-07 Guilherme Blanco: Expanded extended rules.
Line 443: Line 451:
   * 2010-03-22 Guilherme Blanco: Initial RFC creation.   * 2010-03-22 Guilherme Blanco: Initial RFC creation.
  
 +===== Comments =====
 +  * laruence : I have already got a similar loader implemented in Yaf, called Yaf_Loader, you can found the source here: [[http://svn.php.net/viewvc/pecl/yaf/trunk/yaf_loader.c?view=markup]]
rfc/splclassloader.1320763209.txt.gz · Last modified: 2017/09/22 13:28 (external edit)