user221931
user221931

Reputation: 1852

What is the preffered level inside controllers to place App::uses?

I know App::uses does lazy-loading, but if we are going to use i.e. CakeTime in a small part of our code (like inside an if statement that is called 1/3 of the times the method is called) then what would be the best place to put App::uses('CakeTime', 'Utility')?

The options are:

  1. AppController
  2. MyController
  3. MyController::methodName
  4. MyController::methodName inside the if statement (where it is actually going to be used).

I'm putting it in 4 as I guess there must be some obvious overhead (even if really small) but I don't see any reason for it to be present in every call of the controller. My colleague says 2 because it "we might need it elsewhere in the future and it lazyloads so it's not a problem". My answer to that is that if we need it elsewhere then we should redefine then where to put it according to the case.

What is your opinion and why?

Upvotes: 0

Views: 74

Answers (1)

mark
mark

Reputation: 21743

There is no preferred place, only a "correct" place. Always in the file where the class actually uses it.

So if you use CakeTime in your MyController, there is only the top of this very same file where you may put it.

Besides the technical aspect here it is also wise to do that for the coming up 3.0 version. If you ever want to migrate (and thus to use namespace declarations) you will be more than grateful if you places those then former App::uses() statements in the correct places.

Upvotes: 2

Related Questions