无法声明ABC类,因为该名称已被使用

时间:2019-07-16 13:20:32

标签: php codeigniter-3

在列出项目中的所有控制器名称和函数时,出现诸如Fatal error: Cannot declare class ABC, because the name is already in use in之类的错误。为了让我从周围环境中获得一个库并将其放置。库看起来像这样

    <?php
    if (!defined('BASEPATH'))
        exit('No direct script access allowed');
    /***
     * File: (Codeigniterapp)/libraries/Controllerlist.php
     * 
     * A simple library to list al your controllers with their methods.
     * This library will return an array with controllers and methods
     * 
     * The library will scan the "controller" directory and (in case of) one (1) subdirectory level deep
     * for controllers
     * 
     * Usage in one of your controllers:
     * 
     * $this->load->library('controllerlist');
     * print_r($this->controllerlist->getControllers());
     * 
     * @author Peter Prins 
     */
    class ControllerList {


        /**
         * Codeigniter reference 
         */
        private $CI;

        /**
         * Array that will hold the controller names and methods
         */
        private $aControllers;

        // Construct
        function __construct() {


            // Get Codeigniter instance 
            $this->CI = get_instance();

            // Get all controllers 
              $this->setControllers();
        }

        /**
         * Return all controllers and their methods
         * @return array
         */
        public function getControllers() {
            return $this->aControllers;
        }

        /**
         * Set the array holding the controller name and methods
         */
        public function setControllerMethods($p_sControllerName, $p_aControllerMethods) {
            $this->aControllers[$p_sControllerName] = $p_aControllerMethods;
        }

        /**
         * Search and set controller and methods.
         */
        private function setControllers() {

            // Loop through the controller directory
            foreach(glob(APPPATH . 'controllers/*') as $controller) {

                // if the value in the loop is a directory loop through that directory
                if(is_dir($controller)) {
                    // Get name of directory

                    $dirname = basename($controller, 'EXT');

                    // Loop through the subdirectory
                    foreach(glob(APPPATH . 'controllers/'.$dirname.'/*') as $subdircontroller) {
                        // Get the name of the subdir
                        $subdircontrollername = basename($subdircontroller, EXT);

                        // Load the controller file in memory if it's not load already
                        if(!class_exists($subdircontrollername)) {              
                            $this->CI->load->file($subdircontroller);
                        }                   
                        // Add the controllername to the array with its methods
                        $aMethods = get_class_methods($subdircontrollername);
                        $aUserMethods = array();
                        foreach($aMethods as $method) {
                            if($method != '__construct' && $method != 'get_instance' && $method != $subdircontrollername) {
                                $aUserMethods[] = $method;
                            }
                        }
                        $this->setControllerMethods($subdircontrollername, $aUserMethods);                                      
                    }
                }
                else if(pathinfo($controller, PATHINFO_EXTENSION) == "php"){

                    // value is no directory get controller name                
                    $controllername = basename($controller, 'EXT');

                    // Load the class in memory (if it's not loaded already)
                    if(!class_exists($controllername)) {
                        var_dump($controller);
                        $this->CI->load->file($controller);
                    }               

                    // Add controller and methods to the array
                    $aMethods = get_class_methods($controllername);
                    //var_dump($aMethods);      
                    $aUserMethods = array();
                    if(is_array($aMethods)){
                        foreach($aMethods as $method) {
                            if($method != '__construct' && $method != 'get_instance' && $method != $controllername) {
                                $aUserMethods[] = $method;
                            }
                        }
                    }

                    $this->setControllerMethods($controllername, $aUserMethods);                                
                }
            }   
        }
    }
    // EOF

$this->CI->load->file($controller);行中出现错误。当我在控制器中调用此库时,出现标题中提到的错误。这是php版本的问题,这是在告诉控制器类名是已经被调用,因此无法再次调用。

0 个答案:

没有答案