CakePHP 2.X - loading custom class in app/Lib dir

rynop picture rynop · Feb 8, 2012 · Viewed 20k times · Source

I have a class Called MyClass. It lives in app/Lib/MyDir/MyClass. I'd like to use App:uses() to load it, but can't get it to work.

In CakePHP 1.3 I would load it via:

App::import('Lib', 'MyDir/MyClass');  //this still works in CakePHP 2.1

In CakePHP 2.1 I'm trying to do:

App::uses('MyClass', 'Lib/MyDir');

When I try to 'new' up MyClass I get Class 'MyClass' not found.

Is it not possible to use App::uses on custom Lib classes? I can't continue to use App::import() because if 'App::import('Lib', 'MyDir/MyClass');' appears 2x in the code path I get a 'Cannot redeclare class' error in lib/Cake/Core/App.php on line 531

what am I doing wrong?

Edit: so if I do App::uses('MyClass', 'MyDir'); it works. Not sure if thats how its supposed to behaive, but reporting bug.

Answer

mark picture mark · Feb 9, 2012

did you try

App::uses('MyClass', 'MyDir');

? since "Lib" itself can be seen as a base directory

PS: you are even supposed to group everything inside Lib in subfolders (packages) similar to the core.