返回类名,但get_class_methods使用spl_autoload_register返回Null

时间:2018-03-16 22:02:57

标签: php

我正在尝试阅读存储在文件夹中的一些类文件并获取它们的方法。

但是,get_class_methods返回null。但是,如果我将server_defines文件和index.php放入v1文件夹,则一切正常。

我不确定发生了什么,因为在我看来(新手)一切都包含在内。

这是文件夹结构

  • 阿比
    • server_defines.php
    • 测试
      • 的index.php
    • V1
      • MyMasterClass.php
      • 运营
        • OneClass.php
        • TwoClass.php
        • ThreeClass.php

server_defines.php

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/" );

测试/ index.php的

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

MyMasterClass.php

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;
        }

    }

1 个答案:

答案 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/>";

        }
...