Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Classes vs 2D arrays

Tags:

arrays

php

class

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.

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


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.

like image 412
Teifion Avatar asked Aug 23 '08 10:08

Teifion


People also ask

When would you use a 2D array?

A two-dimensional array can also be used to store objects, which is especially convenient for programming sketches that involve some sort of “grid” or “board.” Example 13-11 displays a grid of Cell objects stored in a two-dimensional array.

How do you declare a 2D array in class?

Begin Create a class Arr and declare size of array. Inside the class, initialize all the elements by using for loop. Print the all elements. End.

What are the advantages of 2D arrays?

With multidimensional or 2d array, it's easy to access and maintain. You don't have to use various variables for each entity which can reside in a single variable throughout your application. Every variable created takes up a specific resource which when accessed has to be looked-up.


2 Answers

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.

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
    }
}
like image 175
Brian Warshaw Avatar answered Nov 09 '22 19:11

Brian Warshaw


It depends exactly what you mean by 'better'. I'd go for the object oriented way (using classes) because I find it makes for cleaner code (at least in my opinion). However, I'm not sure what the speed penalties might be for that option.

like image 23
robintw Avatar answered Nov 09 '22 17:11

robintw