0

Im building a custom CMS and have setup an autoloader, and have adapted use of namespaces. For the most part things are loading properly, but in certain cases PHP reports that it cannot find the class, the class file has been included.

Once a file is included (using require), it should be instanced as well. The parent controller is instanced, then the child controller attempts to instance a few of its own dependencies.

$this->auth     = new \Modules\Auth\RT\Auth();

This will look for a file at /modules/auth/rt/auth.php, and it does and the class is instanced properly.

The namespace of Auth is:

namespace Modules\Auth\RT;

The auth class tried to load its own dependencies, a model in particular.

$this->auth_model   = new Models\ModelAuth();

Here the file to be included is at /modules/auth/rt/models/modelauth.php It is included successfully, but this is where PHP says I cannot find this class.

Fatal error: Class 'Modules\Auth\RT\ModelAuth' not found in /Users/richardtestani/Documents/ShopOpen-Master/shopopen/modules/auth/rt/auth.php on line 12

What would cause the class from not being instanced even though the file is included? Thanks Rich

Richard Testani
  • 1,474
  • 4
  • 15
  • 29

2 Answers2

0

try this:

$this->auth_model   = new Modules\Auth\RT\Models\ModelAuth();
volkinc
  • 2,143
  • 1
  • 15
  • 19
  • Returns error: Fatal error: Class 'Modules\Auth\RT\Modules\Auth\RT\Models\ModelAuth' not found in /Users/richardtestani/Documents/ShopOpen-Master/shopopen/modules/auth/rt/auth.php on line 12 - seems to concatenate current namespace and path to class. – Richard Testani May 15 '14 at 14:16
  • While I found a solution, I am a little confused on why it works and not the my initial code. Seems having the full path as the namespace fixes this, as well as instancing it in that same name space. So whie the file was loaded, the namespace could not be found, and thus the class was not instanced. – Richard Testani May 20 '14 at 16:17
0

try this:

$this->auth_model = new \Modules\Auth\RT\Models\ModelAuth(); OR $this->auth_model = new Models\ModelAuth();
when you are in this namespace \Modules\Auth\RT

There was a Missing \ , so the code trys to include the namespace twice;

FOR REAL ;)

  • Im still getting the same error. Strange since the file is properly included. Both situations result in same error. I've reviewed my autoloader, and even tested it outside of the application and it loads there. Im not sure what is happening in this situation. – Richard Testani May 20 '14 at 15:28