php - Class not found -


i'm trying write front controller in php.

here code:

<?php  require_once('controller/logincontroller.php'); require_once('view/loginview.php'); require_once('model/usermodel.php');  class frontcontroller {     private $controller;     private $view;      public function __construct(router $router, $routename, $action = null)     {         $route = $router->getroute($routename);          $modelname = $route->model;         $controllername = $route->controller;         $viewname = $route->view;          $model = new $modelname;         $this->controller = new $controllername($model);         $this->view = new $viewname($routename, $model);          if (!empty($action)) $this->controller->{$action}();     }      public function output() {          if (!empty($this->view))         {             return $this->view->output();         }     }  } 

when i'm passing in route object properties follows:

  route-> model = 'usermodel'   route-> view = 'loginview'   route-> controller = 'logincontroller' 

php tells me class usermodel can not found.

however, if replace above static expressions:

 $model = new usermodel();  $this->controller = new logincontroller();  $this->view = new loginview(); 

the code works fine, presumably tells me classes available use in code.

i'm lost. there i'm overlooking?

further inquiries piece of code

if (!class_exists("usermodel")) die("no usermodel"); 

tell me class doesn't exist. how possible can construct new usermodel() , how can fix it?

you have specify namespaces class names stored in variables, since resolved @ runtime:

$class = 'your\full\namespace\path\usermodel'; // ... $object = new $class; 

php can't reliably detect namespace implied dynamic class names.

besides, becomes more , more difficult track dependencies project grows. recommend implementing autoloading system, or use existing solution such composer.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -