简体   繁体   English

静态吸气剂/固定器的缺点

[英]Downside of static getters/setters

I don't know if this question fits this site, but I'll ask anyway: What are the downsides of using static getters? 我不知道这个问题是否适合该网站,但无论如何我都会问:使用静态吸气剂有哪些弊端? For my website, I use CodeIgniter and I coded a "finder" function in MY_Model.php. 对于我的网站,我使用CodeIgniter,并在MY_Model.php中编写了一个“查找器”功能。 The individual Models use this model as a parent and just kind of mirror the database. 各个模型使用此模型作为父模型,并且只是镜像数据库。 I would use my finder method to get an object of a model and then with the static getter - also declared in MY_Model.php - get the attributes. 我将使用finder方法获取模型的对象,然后使用静态吸气剂(也在MY_Model.php中声明)获取属性。 Like so: 像这样:

<?php
$article = Article::find(array(
    'id' => $someId,
));
echo Article::get($article, 'title');

or for example if I have to edit an entry, I can just do it like this: 或例如,如果我必须编辑一个条目,我可以这样做:

<?php
$article = Article::find(array(
    'id' => $someId,
));
Article::set($article, 'title', $theTitle)
       ->set($article, 'text', $theText);
if (!$article->update()) {
    return false;
}
return true;

The first real downside I know is, that it completely goes against the 'Thin Controller, Fat Model' principle, since I'd only declare the variables in the model, mirroring the table fields plus maybe some individual functions. 我知道的第一个实际缺点是,这完全违反了“瘦控制器,胖模型”的原则,因为我只在模型中声明变量,镜像表字段以及某些单独的函数。 But it completely erases the necessity to write getter and setter functions for every attribute. 但这完全消除了为每个属性编写getter和setter函数的必要性。

You could go for magic methods as well and have getters and setters as object methods this way. 您也可以使用魔术方法,并以这种方式将getter和setter作为对象方法。

However, I don't know if I got the point of your question. 但是,我不知道我是否明白你的问题。 In my opinion, what you're trying to do is ORM somehow. 在我看来,您要尝试的是某种方式的ORM。 ORM is not the same as a Model. ORM与模型不同。 Models don't care about things like a persistence layer and reflecting one, they don't even know it. 模型并不关心诸如持久层和反映一个持久层之类的东西,他们甚至都不知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM