在PHP中存储类变量的最佳方法是什么?

时间:2022-04-02 22:44:51

I currently have my PHP class variables set up like this:

我目前的PHP类变量设置如下:

class someThing {

    private $cat;
    private $dog;
    private $mouse;
    private $hamster;
    private $zebra;
    private $lion;

    //getters, setters and other methods
}

But I've also seen people using a single array to store all the variables:

但我也看到人们使用单个数组来存储所有变量:

class someThing {

    private $data = array();

    //getters, setters and other methods
}

Which do you use, and why? What are the advantages and disadvantages of each?

你用哪个,为什么?各有哪些优缺点?

5 个解决方案

#1


Generally, the first is better for reasons other people have stated here already.

一般来说,第一个更好,因为其他人已经在这里说过。

However, if you need to store data on a class privately, but the footprint of data members is unknown, you'll often see your 2nd example combined with __get() __set() hooks to hide that they're being stored privately.

但是,如果您需要私有地在类上存储数据,但数据成员的占用空间未知,您通常会看到第二个示例与__get()__ set()挂钩以隐藏它们是私有存储的。

class someThing {

    private $data = array();

    public function __get( $property )
    {
        if ( isset( $this->data[$property] ) )
        {
            return $this->data[$property];
        }
        return null;
    }

    public function __set( $property, $value )
    {
        $this->data[$property] = $value;
    }
}

Then, objects of this class can be used like an instance of stdClass, only none of the members you set are actually public

然后,此类的对象可以像stdClass的实例一样使用,只有您设置的成员中没有一个实际上是公共的

$o = new someThing()
$o->cow = 'moo';
$o->dog = 'woof';
// etc

This technique has its uses, but be aware that __get() and __set() are on the order of 10-12 times slower than setting public properties directly.

此技术有其用途,但请注意__get()和__set()比直接设置公共属性的速度快10-12倍。

#2


If you're using private $data; you've just got an impenetrable blob of data there... Explicitly stating them will make your life much easier if you're figuring out how a class works.

如果您使用私人$数据;你刚刚得到了一个难以理解的大量数据......如果你弄清楚一个班级是如何运作的,那么明确地陈述它们会让你的生活更轻松。

Another consideration is if you use an IDE with autocomplete - that's not going to work with the 2nd method.

另一个考虑因素是如果您使用具有自动完成功能的IDE - 这不适用于第二种方法。

#3


If code is repetitive, arrays and (foreach) loops neaten things. You need to decide if the "animal" concept in your code is repetitive or not, or if the code needs to dig in to the uniqueness of each member.

如果代码是重复的,那么数组和(foreach)循环就可以了。您需要确定代码中的“动物”概念是否重复,或者代码是否需要深入了解每个成员的唯一性。

If I have to repeat myself more than once, I loop.

如果我不得不多次重复自己,我会循环。

#4


  • Use the first method when you know you need that variable.
  • 当您知道需要该变量时,请使用第一种方法。

  • Use the second method (an array collection of variables) when you have dynamic variable needs.
  • 当您有动态变量需求时,请使用第二种方法(变量的数组集合)。

You can combine these 2 methods, so some variables are hardcoded into your class, while others are dynamic. The hardcoded variables will have preference compared with magic methods.

您可以将这两种方法结合起来,因此一些变量会硬编码到您的类中,而其他变量则是动态的。与魔术方法相比,硬编码变量具有偏好。

#5


I prefer the first method, for a few reasons:

我更喜欢第一种方法,原因如下:

In a good IDE, the class properties show up, even if private/protected It's easier to see what has already been defined, reducing the chance you store the same information twice. If the proverbial bus hits you on the way home, it's a lot simpler for another developer to come in and read your code. And while it doesn't apply to private var, it does to protected vars, in classes that extend this class, you really should try to avoid the second method for pure readability.

在一个好的IDE中,即使私有/受保护,也会显示类属性。更容易看到已经定义的内容,减少了两次存储相同信息的可能性。如果众所周知的公共汽车在回家的路上遇到你,那么另一个开发人员进入并阅读你的代码要简单得多。虽然它不适用于私有var,但它对受保护的变量有效,在扩展此类的类中,你真的应该尝试避免第二种纯可读性方法。

Also, as a side note, I almost always choose protected over private unless I have a very specific reason to make it private.

另外,作为旁注,除非我有一个非常具体的理由将其设为私有,否则我几乎总是选择受保护而不是私有。

The only time I'd probably use the second method was if I was storing a collection of many of one kind of thing.

我可能只使用第二种方法的唯一一次是如果我存储了许多一种东西的集合。

#1


Generally, the first is better for reasons other people have stated here already.

一般来说,第一个更好,因为其他人已经在这里说过。

However, if you need to store data on a class privately, but the footprint of data members is unknown, you'll often see your 2nd example combined with __get() __set() hooks to hide that they're being stored privately.

但是,如果您需要私有地在类上存储数据,但数据成员的占用空间未知,您通常会看到第二个示例与__get()__ set()挂钩以隐藏它们是私有存储的。

class someThing {

    private $data = array();

    public function __get( $property )
    {
        if ( isset( $this->data[$property] ) )
        {
            return $this->data[$property];
        }
        return null;
    }

    public function __set( $property, $value )
    {
        $this->data[$property] = $value;
    }
}

Then, objects of this class can be used like an instance of stdClass, only none of the members you set are actually public

然后,此类的对象可以像stdClass的实例一样使用,只有您设置的成员中没有一个实际上是公共的

$o = new someThing()
$o->cow = 'moo';
$o->dog = 'woof';
// etc

This technique has its uses, but be aware that __get() and __set() are on the order of 10-12 times slower than setting public properties directly.

此技术有其用途,但请注意__get()和__set()比直接设置公共属性的速度快10-12倍。

#2


If you're using private $data; you've just got an impenetrable blob of data there... Explicitly stating them will make your life much easier if you're figuring out how a class works.

如果您使用私人$数据;你刚刚得到了一个难以理解的大量数据......如果你弄清楚一个班级是如何运作的,那么明确地陈述它们会让你的生活更轻松。

Another consideration is if you use an IDE with autocomplete - that's not going to work with the 2nd method.

另一个考虑因素是如果您使用具有自动完成功能的IDE - 这不适用于第二种方法。

#3


If code is repetitive, arrays and (foreach) loops neaten things. You need to decide if the "animal" concept in your code is repetitive or not, or if the code needs to dig in to the uniqueness of each member.

如果代码是重复的,那么数组和(foreach)循环就可以了。您需要确定代码中的“动物”概念是否重复,或者代码是否需要深入了解每个成员的唯一性。

If I have to repeat myself more than once, I loop.

如果我不得不多次重复自己,我会循环。

#4


  • Use the first method when you know you need that variable.
  • 当您知道需要该变量时,请使用第一种方法。

  • Use the second method (an array collection of variables) when you have dynamic variable needs.
  • 当您有动态变量需求时,请使用第二种方法(变量的数组集合)。

You can combine these 2 methods, so some variables are hardcoded into your class, while others are dynamic. The hardcoded variables will have preference compared with magic methods.

您可以将这两种方法结合起来,因此一些变量会硬编码到您的类中,而其他变量则是动态的。与魔术方法相比,硬编码变量具有偏好。

#5


I prefer the first method, for a few reasons:

我更喜欢第一种方法,原因如下:

In a good IDE, the class properties show up, even if private/protected It's easier to see what has already been defined, reducing the chance you store the same information twice. If the proverbial bus hits you on the way home, it's a lot simpler for another developer to come in and read your code. And while it doesn't apply to private var, it does to protected vars, in classes that extend this class, you really should try to avoid the second method for pure readability.

在一个好的IDE中,即使私有/受保护,也会显示类属性。更容易看到已经定义的内容,减少了两次存储相同信息的可能性。如果众所周知的公共汽车在回家的路上遇到你,那么另一个开发人员进入并阅读你的代码要简单得多。虽然它不适用于私有var,但它对受保护的变量有效,在扩展此类的类中,你真的应该尝试避免第二种纯可读性方法。

Also, as a side note, I almost always choose protected over private unless I have a very specific reason to make it private.

另外,作为旁注,除非我有一个非常具体的理由将其设为私有,否则我几乎总是选择受保护而不是私有。

The only time I'd probably use the second method was if I was storing a collection of many of one kind of thing.

我可能只使用第二种方法的唯一一次是如果我存储了许多一种东西的集合。