我正在尝试阅读存储在文件夹中的一些类文件并获取它们的方法。
但是,get_class_methods
返回null。但是,如果我将server_defines
文件和index.php
放入v1
文件夹,则一切正常。
我不确定发生了什么,因为在我看来(新手)一切都包含在内。
这是文件夹结构
error_reporting(E_ALL ^ E_STRICT ^ E_DEPRECATED);
ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
if (!defined("SERVER_DOCUMENT_ROOT")) {
define("SERVER_DOCUMENT_ROOT", $_SERVER['DOCUMENT_ROOT'] . "/");
}
define( "SERVER_API_PATH", SERVER_DOCUMENT_ROOT . "v1/" );
define( "SERVER_API_OPERATIONS_PATH", SERVER_API_PATH . "Operations/" );
require_once( $_SERVER[ 'DOCUMENT_ROOT' ] . "/server_defines.php" );
spl_autoload_register( function ( $class ) {
$sFile = SERVER_API_PATH . str_replace( '\\', '/', $class ) . '.php';
if (file_exists( $sFile )) {
echo "requiring file: " . $sFile . "<br/>";
require $sFile;
}
} );
var_dump(MyMasterClass::getClassesAndMethods());
*哪个输出*
requiring file: /Users/myname/Documents/GitHub/myproject/Api/v1/MyMasterClass.php
folder_contents: array(3) {
[0]=>
string(80) "/Users/myname/Documents/GitHub/myproject/Api/v1/Operations/OneClass.php"
[1]=>
string(75) "/Users/myname/Documents/GitHub/myproject/Api/v1/Operations/TwoClass.php"
[2]=>
string(75) "/Users/myname/Documents/GitHub/myproject/Api/v1/Operations/ThreeClass.php"
}
basename: OneClass
get_class_methods: NULL
basename: TwoClass
get_class_methods: NULL
basename: ThreeClass
get_class_methods: NULL
class MyMasterClass {
public function __toString () {
return $this->result;
}
public function __construct ( ) {
}
public static function getClassesAndMethods () {
$api = new API();
return $api->buildClassesAndMethods();
}
private function buildClassesAndMethods () {
$folder_contents = glob( SERVER_API_OPERATIONS_PATH . '*.{php}', GLOB_BRACE );
echo "<br/>folder_contents: ";
var_dump( $folder_contents );
echo "<br/>";
$collection = array();
foreach ($folder_contents as $name) {
$name = basename( $name, ".php" );
echo "<br/>basename: " . $name . "<br/>";
echo "<br/>get_class_methods: ";
var_dump( get_class_methods( $name ) );
echo "<br/>";
}
return $collection;
}
}
答案 0 :(得分:0)
当您将类移动到子文件夹时,您自动加载器无法加载这些类,因为它仅涵盖SERVER_API_PATH
中的文件。尝试使用require_once
手动更新自动加载方法或加载类。
尝试
...
foreach ($folder_contents as $name) {
require_once($name);
$name = basename( $name, ".php" );
echo "<br/>basename: " . $name . "<br/>";
echo "<br/>get_class_methods: ";
var_dump( get_class_methods( $name ) );
echo "<br/>";
}
...