开发者

Is it ok to assign model as class property for later use?

开发者 https://www.devze.com 2023-02-14 22:36 出处:网络
Let\'s imagine I have controller where are a lot of methods that us开发者_StackOverflowe the same model. Is it ok to do like this... http://codepad.org/bSSMFpzp

Let's imagine I have controller where are a lot of methods that us开发者_StackOverflowe the same model. Is it ok to do like this... http://codepad.org/bSSMFpzp

Before I did like I create new variable in each method... I just try to follow DRY principles. ))


I don't think it is the most helpful call, the factory() calls can be chained from so it can be pretty simple anyway. Additionally you don't need to have the variables as object properties in the example code, you could be using $alln instead of $this->alln. Which I feel makes code more readable.

That being said, if you have a reason to make it an object property in the other actions, yes its okay to assign an object to the controller that you may use in multiple actions. You do have to be sure you're not going to replace anything that is already in the parent class that you might replace.

So if you are just using the Controller provided by Kohana, then don't use the variables request or response.


I try and avoid that, in order to make my actions more self contained and easier to read.

$this->foo->thing();

is much less easy to read than:

Model::factory('foo')->thing();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号