Switching to namespaces

I am trying to convert my project to use namespacing, however the controller that gets called is based on the module and action. Using the older way of:

    $name     = 'Controller_'.ucfirst ($module).'_'.ucfirst ($action);
    new $name($this);

Would call the class I needed via this auto loader:

function __autoload ($className) {
    
        $className = str_replace ('_', '/', $className);
    
        if (file_exists (_LIBRARY.$className.'.php')) {
            require (_LIBRARY.$className.'.php');
        } else {
            throw new Exception($className.' could not be loaded', 404);
        }
    }

The new auto loader I’m using is this:

spl_autoload_register(function ($className) {
        $className = ltrim($className, '\\\\');
        $fileName = '';
        $namespace = '';
        if ($lastNsPos = strripos($className, '\\\\')) {
            $namespace = substr($className, 0, $lastNsPos);
            $className = substr($className, $lastNsPos + 1);
            $fileName = str_replace('\\\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
        }
        $fileName = _LIBRARY . $fileName . $className . '.php';
    
        if (file_exists($fileName)) {
            require $fileName;
    
            return true;
        }
    
        return false;
    });

As part of the conversion, my index and controller looks like this:

index.php

use System\\Controller as Controller;
    $system = new Controller();
    $system->_path($_GET['module'], $_GET['action']);

System/Controller.php: path method:

public function _path ($module, $action) {
            $this->privateLog(__FUNCTION__);
    
            $name     = 'Controller_'.ucfirst ($module).'_'.ucfirst ($action);
            $file     = str_replace ('_', '/', $name).'.php';
            $filePath = _LIBRARY.$file;
    
            if (!file_exists ($filePath)) {
                throw new Exception('Route is missing', 404);
            } else {
    
                $this->controller = (object) array (
                    'name'     => $name,
                    'file'     => $file,
                    'filePath' => $filePath,
                    'instance' => new Controller($this)
                );
            }
        }



Controller/Profile/Index.php

namespace Controller\\Profile;
    
    use System\\Controller;
    use MongoId;
    use MongoDate;
    use Exception;
    use MongoCursorException;
    
    
    class Index extends Controller {


Controller/User/Index.php


    namespace Controller\\User;
    
    use System\\Controller;
    use MongoId;
    use System\\Password;
    use MongoDate;
    use Exception;
    use MongoCursorException;
    
    
    class Index extends Controller {

I need to rewrite the _path method to dynamically call the right controller via module / action passed, I have no idea how to do that.

I have tried:

use {$module}\\{$action} as ClassCall;

but I get

Parse error: syntax error, unexpected 'use' (T_USE) in

I fear I’m completely off base anyway. Any help is appreciated. Thank you

You cannot have “use” into your method.
The autoload method should return the exact path of the needed class

<?php
$finalClassName = "{$module}\\{$action}\\$YOURCLASSNAME";
$class = new $finalClassName;

$finalClassName = "$module\\\\$action";
            $this->controller = (object) array (
                'name'     => $name,
                'file'     => $file,
                'filePath' => $filePath,
                'instance' => new $finalClassName($this)
            );

Returns
Fatal error: Class ‘User\Index’ not found in /library/System/Controller.php on line 243 which is ‘instance’ => new $finalClassName($this)


EDIT: FIXED

            $finalClassName = array('Controller', $module, $action);
            $finalClassName = implode('\\\\', $finalClassName);
            $this->controller = (object) array (
                'name'     => $name,
                'file'     => $file,
                'filePath' => $filePath,
                'instance' => new $finalClassName($this)
            );

Thanks!