SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    319
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:
    Code PHP:
        $name     = 'Controller_'.ucfirst ($module).'_'.ucfirst ($action);
        new $name($this);


    Would call the class I needed via this auto loader:
    Code PHP:
    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:
    Code PHP:
    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
    Code PHP:
    use System\Controller as Controller;
        $system = new Controller();
        $system->_path($_GET['module'], $_GET['action']);


    System/Controller.php: path method:
    Code PHP:
    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
    Code 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:
    Code PHP:
    use {$module}\{$action} as ClassCall;


    but I get
    Code PHP:
    Parse error: syntax error, unexpected 'use' (T_USE) in


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

  2. #2
    SitePoint Addict bronze trophy vectorialpx's Avatar
    Join Date
    Dec 2012
    Location
    Bucharest
    Posts
    247
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    You cannot have "use" into your method.
    The autoload method should return the exact path of the needed class

    PHP Code:
    <?php
    $finalClassName 
    "{$module}\{$action}\$YOURCLASSNAME";
    $class = new $finalClassName;
    Be nice to nerds. Chances are you'll end up working for one - Bill Gates
    > photos | admin panel

  3. #3
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    319
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    $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)

  4. #4
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    319
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by azz0r_wugg View Post
    Code:
    $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


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

    Thanks!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •