静态方法获取-这是不好的做法吗? [英] Static method get - is this bad practice?

查看:86
本文介绍了静态方法获取-这是不好的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与同事讨论了这是否是不好的做法.现在,我在网上找不到直接的示例.

Had a discussion with a colleague about wether this is bad practice or not. Now I can not find immediate examples of this online.

我们有很多数据库对象映射器,并称其为函数

We have a lot of database object mappers and call it's functions like so

(示例)-setId方法从数据库获取行并将其设置为预定义属性

(example) - the setId method get's the row from the database and set's it to predefined propertys

class Person {

    public static function get($id) {
        $object = new Person;
        $object->setId($id);
        return $object;
    }
}

使用这种方式,我们可以使用以下简单的结构:(例如,我们从帖子中获得ID)

Using it like this we can use simple constructions like this: (where we got the id from for-example a post)

$person = Person::get($id);

代替

$person = new Person;
$person->setId($id);

现在,我的直觉告诉我这是不好的做法.但我无法解释.也许有人可以解释为什么这是一个好习惯,或者不是一个坏习惯

Now, my instinct tells me this is bad practice. But I can not explain it. Maybe someone here can explain why this is, or is not bad practice

以下是我们如何使用它的其他一些示例.我们主要将其用于吸气剂. (只是名称,而不是代码.几乎所有名称都只运行一个查询,该查询可以返回1个对象,然后使用结果的ID来使用setId方法)

Here are some other examples how we use it. we mainly use it for getters. (just the names, not the code. Almost all of them just run a query, which can return 1 object and then use the id of the result to use the setId method)

class CatalogArticle {
   public static function get($id) { }
   public static function getByArticlenumber($articlenumber) {} //$articlenumber is unique in the database
   public static function getRandom() {} //Runs a query returning a random row
}

推荐答案

这不是可怕的说法.它是工厂方法设计模式的实现.原则上来说还不错.

This isn't horrible persay. It's an implementation of a Factory Method design pattern. It's not bad at all in principle.

但是,在您的特定示例中,它实际上并没有做任何重要的事情,因此我不确定是否有必要.您可以通过向id的构造函数添加一个(可能是可选的)参数来消除此需求.这样,任何人都可以调用$foo = new Person($id);而不需要显式的工厂.

However, in your specific example, it's not really doing anything significant, so I'm not so sure if it's necessary. You could eliminate the need by taking a (perhaps optional) parameter to the constructor for the id. Then anyone could call $foo = new Person($id); rather than needing an explicit factory.

但是,如果实例化很复杂,或者您希望能够构建只能由逻辑确定的几种不同的人员类型,则工厂方法可能会更好.例如,假设您需要通过某个参数确定要实例化的人员的类型.然后,在Person上使用工厂方法将是适当的.该方法将确定要加载的类型",然后实例化该类.

But if the instantiation is complex, or you want the ability to build several different people types that can only be determined by logic, a factory method may work better. For example, let's say you need to determine the type of person to instantiate by some parameter. Then, a factory method on Person would be appropriate. The method would determine what "type" to load, and then instantiate that class.

一般来说,静态数据很难测试,并且不允许像实例那样进行多态更改.它们还在代码中的类之间创建硬依赖关系.它们不是可怕的,但是如果您要使用它们,则应该真正考虑一下.一种选择是使用生成器抽象工厂.这样,您将创建一个builder/factory的实例,然后让该实例确定如何实例化生成的类...

Statics in general are hard to test and don't allow for polymorphic changes like an instance would. They also create hard dependencies between classes in the code. They are not horrible, but you should really think about it if you want to use one. An option would be to use a Builder or a Abstract Factory. That way, you create an instance of the builder/factory, and then let that instance determine how to instantiate the resulting class...

另一个说明.我将该方法从Person::get()重命名为在语义上更合适的名称.也许Person::getInstance()或其他合适的东西.

One other note. I would rename that method from Person::get() to something a little more semantically appropriate. Perhaps Person::getInstance() or something else appropriate.

这篇关于静态方法获取-这是不好的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆