0

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?

4

1 に答える 1

2

好ましい場所はなく、「正しい」場所だけです。クラスが実際に使用するファイルに常にあります。

そのため、MyController で CakeTime を使用する場合、これを配置できるのはこのまったく同じファイルの先頭だけです。

ここでの技術的な側面に加えて、今後の 3.0 バージョンでそれを行うことも賢明です。移行する (したがって名前空間宣言を使用する) 場合は、以前の App::uses() ステートメントを正しい場所に配置していただければ、非常にありがたいことです。

于 2013-08-31T17:56:49.340 に答える