班VS二维数组 [英] Classes vs 2D arrays

查看:144
本文介绍了班VS二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是更好的在PHP中,一个二维数组或类使用?我已经包括了我所说的意思的例子。

Which is better to use in PHP, a 2D array or a class? I've included an example of what I mean by this.

// Using a class
class someClass
{
    public  $name;
    public  $height;
    public  $weight;

    function __construct($name, $height, $weight)
    {
        $this -> name       = $name;
        $this -> height = $height;
        $this -> weight = $weight;
    }
}

$classArray[1] = new someClass('Bob', 10, 20);
$classArray[2] = new someClass('Fred', 15, 10);
$classArray[3] = new someClass('Ned', 25, 30);


// Using a 2D array
$normalArray[1]['name'] = 'Bob';
$normalArray[1]['height']   = 10;
$normalArray[1]['weight']   = 20;

$normalArray[2]['name'] = 'Fred';
$normalArray[2]['height']   = 15;
$normalArray[2]['weight']   = 10;

$normalArray[3]['name'] = 'Ned';
$normalArray[3]['height']   = 25;
$normalArray[3]['weight']   = 30;


假设有人不站出来,表明类是太慢了,它看起来像阶级的胜利。


Assuming that somebody doesn't come out and show that classes are too slow, it looks like class wins.

我已经不知道哪个回答,我应该接受我只是upvoted所有的人。

I've not idea which answer I should accept to I've just upvoted all of them.

现在我已经使用了二维数组(这个问题被张贴之前写的),现在一用一类写了两个几乎相同的网页,之一,我必须说,类产生的好得多code。我不知道多少开销将如何产生,但是我怀疑这将对手的提高到code本身。

And I have now written two near identical pages, one using the 2D array (written before this question was posted) and now one using a class and I must say that the class produces much nicer code. I have no idea how much overhead is going to be generated but I doubt it will rival the improvement to the code itself.

感谢您的帮助,让我一个更好的程序员。

Thank you for helping to make me a better programmer.

推荐答案

您已经具有上述构造的一流是大多数人会用一个什么样的结构的其他语言。我不知道什么样的性能影响是PHP,但我怀疑对象实例化可能更昂贵这里,如果只有一点点。

The "class" that you've constructed above is what most people would use a struct for in other languages. I'm not sure what the performance implications are in PHP, though I suspect instantiating the objects is probably more costly here, if only by a little bit.

话虽这么说,如果成本是比较低的,这是一个有点更易于管理的对象,在我看来。

That being said, if the cost is relatively low, it IS a bit easier to manage the objects, in my opinion.

我只是说的基础上的标题和你的问题,但以下几点:
请记住,类提供的方法和访问控制的优势,以及。因此,如果你希望确保人们不改变权重负数,你可以使重量私人领域,并提供一些访问方法,如 getWeight() setWeight()。在 setWeight(),你可以做一些值检查,像这样:

I'm only saying the following based on the title and your question, but: Bear in mind that classes provide the advantage of methods and access control, as well. So if you wanted to ensure that people weren't changing weights to negative numbers, you could make the weight field private and provide some accessor methods, like getWeight() and setWeight(). Inside setWeight(), you could do some value checking, like so:

public function setWeight($weight)
{
    if($weight >= 0)
    {
        $this->weight = $weight;
    }
    else
    {
        // Handle this scenario however you like
    }
}

这篇关于班VS二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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