2

I'm using CI the framework, the code below is a generator aim to generate classes public methods list:

$directory = FCPATH.APPPATH.'controllers/';
$exclude = array('shell');

$filter = function ($file, $key, $iterator) use ($exclude) {
    if ($iterator->hasChildren() && !in_array($file->getFilename(), $exclude)) {
        return true;
    }
    return $file->isFile();
};

$innerIterator = new RecursiveDirectoryIterator(
    $directory,
    RecursiveDirectoryIterator::SKIP_DOTS
);

$iterator = new RecursiveIteratorIterator(
    new RecursiveCallbackFilterIterator($innerIterator, $filter)
);

foreach ($iterator as $pathname => $fileinfo) {
    if (!preg_match("/\.(php)*$/i", $pathname, $matches)) {
        continue;
    }

    $last = strrpos($pathname, '/');
    $second_last = strrpos($pathname, '/', $last - strlen($pathname) - 1);
    $last_folder_name = substr($pathname, $second_last + 1, $last - $second_last - 1);
    if($last_folder_name == 'controllers')
    {
        continue;
    }

    require_once $pathname;

    $module_name = & $last_folder_name;
    $controller_name = ucfirst($fileinfo->getBasename('.php'));

    $reflection_class = new ReflectionClass($controller_name);
}

I use this code to unset variables:

unset($pathname);
unset($controller_name);
unset($reflection_class);
continue;

It seems I unset the object, but it still says:

Fatal error: Cannot redeclare class Index in ...
Phoenix
  • 1,055
  • 1
  • 12
  • 27
  • It means you've already created or has a class with a similar name. See this for more info: http://stackoverflow.com/questions/708140/php-fatal-error-cannot-redeclare-class – Aviram Jul 26 '15 at 12:16
  • @Aviram I know, and I've searched Google and found this answer, but my problem is, I want to have the two `Index` classes, not only one. Because I want to generate the two classes public method names. – Phoenix Jul 26 '15 at 12:19
  • As far as I know, it is impossible to redeclare a class in PHP. You will have to use a different name. – Aviram Jul 26 '15 at 12:21

1 Answers1

0

You cannot undeclare a class once it's declared. If you want to use multiple classes with the same name, look into PHP Namespaces

Stephen
  • 18,597
  • 4
  • 32
  • 33