2012-12-06 58 views
3

当在类中实现名称空间时,在加载类为spl_autoload_register时出现问题。带有名称空间的PHP类无法通过spl_autoload_register加载?

类下面autoloader,但我没有问题,加载任何类时使用没有命名空间,

class autoloader 
{ 
    /** 
    * Set the property. 
    */ 
    public $directory; 
    public $recursive; 

    /** 
    * Receive the supplied data. 
    * @string $directory 
    * @array $recursive default: models 
    */ 
    public function __construct($directory, $recursive = array('search' => 'models')) 
    { 
     # Store the data into the property. 
     $this->directory = $directory; 
     $this->recursive = $recursive; 

     # When using spl_autoload_register() with class methods, it might seem that it can use only public methods, though it can use private/protected methods as well, if registered from inside the class: 
     spl_autoload_register(array($this,'get_class')); 

    } 

    private function get_class($class_name) 
    { 
     # List all the class directories in the array. 
     if ($this->recursive) 
     { 
      $array_directories = self::get_recursive_directory($this->directory); 
     } 
     else 
     { 
      if (is_array($this->directory)) $array_directories = $this->directory; 
      else $array_directories = array($this->directory); 
     } 

     # Set the class file name. 
     $file_name = 'class_'.strtolower($class_name).'.php'; 

     # Loop the array. 
     foreach($array_directories as $path_directory) 
     { 
      if(file_exists($path_directory.$file_name)) 
      { 
       # There is no need to use include/require_once. Autoload is a fallback when the system can't find the class you are instantiating. 
       # If you've already included it once via an autoload then the system knows about it and won't run your autoload method again anyway. 
       # So, just use the regular include/require - they are faster. 
       include $path_directory.$file_name; 
      } 
     } 

    } 

    # However the memory consumption of this can be huge if you have a very large directory tree with only a few matches. 
    # @source: http://stackoverflow.com/questions/9294543/search-and-list-specific-directories-only 
    public function get_recursive_directory($directory) 
    { 
     # Create an object that allows us to iterate directories recursively. 
     # Stolen from here: 
     # http://www.php.net/manual/en/class.recursivedirectoryiterator.php#102587 
     $iterator = new RecursiveIteratorIterator 
        (
         new RecursiveDirectoryIterator($directory), 
         RecursiveIteratorIterator::CHILD_FIRST 
        ); 

     # This will hold the result. 
     $result = array(); 

     # Loop the directory contents. 
     foreach ($iterator as $path) 
     { 

      # If object is a directory and matches the search term ('models')... 
      if ($path->isDir() && $path->getBasename() === $this->recursive['search']) 
      { 

       # Add it to the result array. 
       # Must replace the slash in the class - dunno why! 
       $result[] = str_replace('\\', '/', $path).'/'; 
       //$result[] = (string) $path . '/'; 

      } 

     } 

     # Return the result in an array. 
     return $result; 
    } 
} 

一个tag类的实例,

namespace hello; 

class tag extends \core 
{ 
} 

现在通过加载类autoloader类,

# Autoload the classes from the specific folder. 
$autoloader = new autoloader("C:/wamp/www/website/local/applications/master/sides/models/", $recursive = false); 

# Instantiate the tag. 
$tag = new hello\tag($connection); 

结果,

致命错误:类 '你好\标签' 在 C未发现:\ WAMP \ WWW \网站\本地\应用程序\主\边\意见\ tags.php 在线

任何想法,我怎么能解决我的autoloader类,这样我可以加载的类是否有一个命名空间或不?

编辑:

,我保留类和类命名的文件夹,

C:\wamp\www\website\local\applications\master\sides\models\ 

class_tag.php 
class_something.php 
+0

向我们展示包含类的文件的命名。如果我正确理解自动加载器,它应该是class_tag.php。你也应该看看[PSR-0](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md)autoloder –

+0

@BenjaminPaap请看看我上面的编辑。谢谢。 – laukok

回答

2

你的错误是在你的get_class()方法。 $class_name包含具有其名称空间的完全限定类名。在你的情况下,你必须从你的类名剥离命名空间。

$file_name = 'class_'.strtolower(array_pop(explode('\\', $class_name))).'.php'; 

我强烈建议使用PSR-0自动加载标准。如果您将使用任何库,则很可能他们使用的是相同的标准,而您只有一个自动加载器。

+0

对不起,这是我以前的编辑错误。我现在纠正了。谢谢。 – laukok

+0

更新了我的答案。 –

+0

感谢您的更新。您的答案现在可以在加载课程中很好地工作。谢谢! – laukok

相关问题