tags:

views:

223

answers:

10

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;
+3  A: 

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.

robintw
+1  A: 

robintw
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

It's the speed that I am thinking of mostly, for anything more complex than what I have here I'd probably go with classes but the question is, what is the cost of a class?

Teifion
+7  A: 

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
    }
}
Brian Warshaw
+2  A: 

Generally, I follow this rule:

1) Make it a class if multiple parts of your application use the data structure.

2) Make it a 2D array if you're using it for quick processing of data in one part of your application.

Steve M
+2  A: 

It's the speed that I am thinking of mostly, for anything more complex than what I have here I'd probably go with classes but the question is, what is the cost of a class?

This would seem to be premature optimisation. Your application isn't going to take any real-world performance hit either way, but using a class lets you use getter and setter methods and is generally going to be better for code encapsulation and code reuse.

With the arrays you're incurring cost in harder to read and maintain code, you can't unit test the code as easily and with a good class structure other developers should find it easier to understand if they need to take it on.

And when later on you need to add other methods to manipulate these, you won't have an architecture to extend.

Flubba
+1  A: 

The class that you have is not a real class in OO terms - its just been contructed to take the space of the instance variables.

That said - there propably isnt much issue with speed - its just a style thing in your example.

The intresting bit - is if you contsrtucted the object to be a real "person" class - and thinkng about the other attributes and actions that you may want of the person class - then you would notice not only a style performance - writting code - but also speed performance.

A: 

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.

Teifion
A: 

If your code uses lot of functions that operate on those attributes (name/height/weight), then using class could be a good option.

Imran
A: 

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.

Teifion
A: 

Teifion, if you use classes as a mere replacement for arrays, you are nowhere near OOP. The essence of OOP is that objects have knowledge and responsibility, can actually do things and cooperate with other classes. Your objects have knowledge only and can't do anything else than idly exist, however they seem to be good candidates for persistence providers (objects that know how to store/retrieve themselves into/from database).

Don't worry about performance, too. Objects in PHP are fast and lightweight and performance in general is much overrated. It's cheaper to save your time as a programmer using the right approach than to save microseconds in your program with some obscure, hard to debug and fix piece of code.

Michał Rudnicki