有這個自動加載類自動加載classes
最初,但現在我想自動載入interfaces
和abstracts
爲好。PHP:我怎樣自動加載接口和摘要
所以我做了以下這個answer變化,
$reflection = new ReflectionClass($class_name);
# Return boolean if it is an interface.
if ($reflection->isInterface())
{
$file_name = 'interface_'.strtolower(array_pop($file_pieces)).'.php';
}
else
{
$file_name = 'class_'.strtolower(array_pop($file_pieces)).'.php';
}
我測試,但這個自動加載類不加載所有接口。任何想法,我已經錯過了?
舉例來說,這是我的接口文件,
interface_methods.php
及其內容,
interface methods
{
public function delete();
}
下面是我整個這個自動加載類。
class autoloader
{
/**
* Set the property.
*/
public $directory;
public $recursive;
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);
}
# Determine the class is an interface.
$reflection = new ReflectionClass($class_name);
$file_pieces = explode('\\', $class_name);
# Return boolean if it is an interface.
if ($reflection->isInterface())
{
$file_name = 'interface_'.strtolower(array_pop($file_pieces)).'.php';
}
else
{
$file_name = 'class_'.strtolower(array_pop($file_pieces)).'.php';
}
# Loop the array.
foreach($array_directories as $path_directory)
{
if(file_exists($path_directory.$file_name))
{
include $path_directory.$file_name;
}
}
}
public function get_recursive_directory($directory)
{
$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;
}
}
也許我錯過了一些東西,但是如何在尚未(自動)加載的類/接口上使用'ReflectionClass'? – Uby 2013-03-27 20:58:18
你是對的。也許我不應該在autoload類中使用'ReflectionClass'。但我怎樣才能自動加載接口和摘要是主要問題。 – laukok 2013-03-27 21:01:47
由於它是自動加載的,你不能使用反射,你應該通過類/接口名稱「猜測」文件的位置。通常命名約定是解決方案。 PRS-0可能是您的一個選擇:https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md – Uby 2013-03-27 21:07:43