Reputation: 9267
When Baking some controllers I noticed that on the top of each controller there is a line
App::uses('AppController', 'Controller');
Deleting it does not make any difference, AFA I noticed, everything works fine without that line. Autocomplete works in any case, so it can not be for that purpose. So, what is the meaning of that ? My cake version is 2.5.3.
Thanks
Upvotes: 1
Views: 3936
Reputation: 3133
App::uses is a static method to register the location of a file (that corresponds with a class) to load for the PHP autoloader to be able to load it on-the-fly.
That App::uses('AppController', 'Controller');
line declares that if the AppController class is referenced (or extended, in this case) and the AppController class isn't yet defined, that it should look for the file within the app/Controller
directory and construct the actual filename like so: AppController.php
Part of that is cakePHP convention, part PHP5.
The reason nothing breaks when you remove this line is because that file is probably required somewhere else in the application (another controller, during bootstrap-- so php already has the AppController class in memory.
The reason it's included on the first line is because the controller you baked extends the AppController class. It has a hard dependency on the AppController class being loaded and perhaps in the cake shell or unit testing environments the app controller isn't automatically loaded before hand so the inclusion must occur. App::uses is like telling cake (and PHP) that 'hey, if you're looking for the AppController
class and you don't already have it, you should look in app/Controller directory and just add .php
on the end!' If the file has already been loaded, App::uses would have no effect.
Upvotes: 7