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

<pre><code>// Using a class
class someClass
{
    public  $name;
    public  $height;
    public  $weight;

    function __construct($name, $height, $weight)
    {
        $this -&gt; name       = $name;
        $this -&gt; height = $height;
        $this -&gt; 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;
</code></pre>

<hr>

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

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

<hr>

<p>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.</p>

<p>Thank you for helping to make me a better programmer.</p>

[{'answer_id': 24131, 'author': 'robintw', 'author_id': 1912, 'author_profile': 'https://Stackoverflow.com/users/1912', 'pm_score': 2, 'selected': False, 'text': "<p>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.</p>\n"}, {'answer_id': 24134, 'author': 'Brian Warshaw', 'author_id': 1344, 'author_profile': 'https://Stackoverflow.com/users/1344', 'pm_score': 4, 'selected': True, 'text': '<p>The "class" that you\'ve constructed above is what most people would use a <em>struct</em> 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.</p>\n\n<p>That being said, if the cost is relatively low, it IS a bit easier to manage the objects, in my opinion.</p>\n\n<p>I\'m only saying the following based on the title and your question, but:\nBear 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 <code>weight</code> field private and provide some accessor methods, like <code>getWeight()</code> and <code>setWeight()</code>. Inside <code>setWeight()</code>, you could do some value checking, like so:</p>\n\n<pre><code>public function setWeight($weight)\n{\n    if($weight &gt;= 0)\n    {\n        $this-&gt;weight = $weight;\n    }\n    else\n    {\n        // Handle this scenario however you like\n    }\n}\n</code></pre>\n'}, {'answer_id': 24136, 'author': 'Steve M', 'author_id': 1693, 'author_profile': 'https://Stackoverflow.com/users/1693', 'pm_score': 2, 'selected': False, 'text': "<p>Generally, I follow this rule:</p>\n\n<p>1) Make it a class if multiple parts of your application use the data structure.</p>\n\n<p>2) Make it a 2D array if you're using it for quick processing of data in one part of your application.</p>\n"}, {'answer_id': 24148, 'author': 'Polsonby', 'author_id': 137, 'author_profile': 'https://Stackoverflow.com/users/137', 'pm_score': 2, 'selected': False, 'text': "<blockquote>\n  <blockquote>\n    <p>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?</p>\n  </blockquote>\n</blockquote>\n\n<p>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. </p>\n\n<p>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.</p>\n\n<p>And when later on you need to add other methods to manipulate these, you won't have an architecture to extend.</p>\n"}, {'answer_id': 24171, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>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.</p>\n\n<p>That said - there propably isnt much issue with speed - its just a style thing in your example.</p>\n\n<p>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.</p>\n'}, {'answer_id': 24307, 'author': 'Imran', 'author_id': 1897, 'author_profile': 'https://Stackoverflow.com/users/1897', 'pm_score': 0, 'selected': False, 'text': '<p>If your code uses lot of functions that operate on those attributes (name/height/weight), then using class could be a good option.</p>\n'}, {'answer_id': 34185, 'author': 'Michał Niedźwiedzki', 'author_id': 2169, 'author_profile': 'https://Stackoverflow.com/users/2169', 'pm_score': 0, 'selected': False, 'text': "<p>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).</p>\n\n<p>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.</p>\n"}, {'answer_id': 4571581, 'author': 'Richard Varno', 'author_id': 214891, 'author_profile': 'https://Stackoverflow.com/users/214891', 'pm_score': 0, 'selected': False, 'text': '<p>Most tests that time arrays vs classes only test instancing them.  Once you actually start to do something with them.</p>\n\n<p>I was a "purist" that used only arrays because the performance was SO much better.  I wrote the following code to justify to myself to justify the extra hassle of not using classes (even though they are easier on the programmer)</p>\n\n<p>Let\'s just say I was VERY surprised at the results!</p>\n\n<pre><code>    &lt;?php\n$rx = "";\n$rt = "";\n$rf = "";\n\n$ta = 0; // total array time\n$tc = 0; // total class time\n\n// flip these to test different attributes\n$test_globals = true;\n$test_functions = true;\n$test_assignments = true;\n$test_reads = true;\n\n\n// define class\n\n\nclass TestObject\n{\n  public $a;\n  public $b;\n  public $c;\n  public $d;\n  public $e;\n  public $f;\n\n  public function __construct($a,$b,$c,$d,$e,$f)\n  {\n    $this-&gt;a = $a;\n    $this-&gt;b = $b;\n    $this-&gt;c = $c;\n    $this-&gt;d = $d;\n    $this-&gt;e = $e;\n    $this-&gt;f = $f;\n  }\n\n  public function setAtoB()\n  {\n      $this-&gt;a = $this-&gt;b;\n  }\n}\n\n// begin test\n\necho "&lt;br&gt;test reads: " . $test_reads;\necho "&lt;br&gt;test assignments: " . $test_assignments;\necho "&lt;br&gt;test globals: " . $test_globals;\necho "&lt;br&gt;test functions: " . $test_functions;\necho "&lt;br&gt;";\n\nfor ($z=0;$z&lt;10;$z++)\n{\n    $starta = microtime(true);\n\n    for ($x=0;$x&lt;100000;$x++)\n    {\n        $xr = getArray(\'aaa\',\'bbb\',\'ccccccccc\',\'ddddddddd\',\'eeeeeeee\',\'fffffffffff\');\n\n        if ($test_assignments)\n        {\n            $xr[\'e\'] = "e";\n            $xr[\'c\'] = "sea biscut";\n        }\n\n        if ($test_reads)\n        {\n            $rt = $x[\'b\'];\n            $rx  = $x[\'f\'];\n        }\n\n        if ($test_functions) { setArrAtoB($xr); }\n        if ($test_globals) { $rf = glb_arr(); }\n    }\n    $ta = $ta + (microtime(true)-$starta);\n    echo "&lt;br/&gt;Array time = " . (microtime(true)-$starta) . "\\n\\n";\n\n\n    $startc = microtime(true);\n\n    for ($x=0;$x&lt;100000;$x++)\n    {\n        $xo = new TestObject(\'aaa\',\'bbb\',\'ccccccccc\',\'ddddddddd\',\'eeeeeeee\',\'fffffffffff\');\n\n        if ($test_assignments)\n        {\n            $xo-&gt;e = "e";\n            $xo-&gt;c = "sea biscut";\n        }\n\n        if ($test_reads)\n        {\n            $rt = $xo-&gt;b;\n            $rx = $xo-&gt;f;\n        }\n\n        if ($test_functions) { $xo-&gt;setAtoB(); }\n        if ($test_globals) { $xf = glb_cls(); }\n    }\n\n    $tc = $tc + (microtime(true)-$startc);\n    echo "&lt;br&gt;Class time = " . (microtime(true)-$startc) . "\\n\\n";\n\n    echo "&lt;br&gt;";\n    echo "&lt;br&gt;Total Array time (so far) = " . $ta . "(100,000 iterations) \\n\\n";\n    echo "&lt;br&gt;Total Class time (so far) = " . $tc . "(100,000 iterations) \\n\\n";\n    echo "&lt;br&gt;";\n\n}\necho "TOTAL TIMES:";\necho "&lt;br&gt;";\necho "&lt;br&gt;Total Array time = " . $ta . "(1,000,000 iterations) \\n\\n";\necho "&lt;br&gt;Total Class time = " . $tc . "(1,000,000 iterations)\\n\\n";\n\n\n// test functions\n\nfunction getArray($a,$b,$c,$d,$e,$f)\n{\n    $arr = array();\n    $arr[\'a\'] = $a;\n    $arr[\'b\'] = $b;\n    $arr[\'c\'] = $c;\n    $arr[\'d\'] = $d;\n    $arr[\'d\'] = $e;\n    $arr[\'d\'] = $f;\n    return($arr);\n}\n\n//-------------------------------------\n\nfunction setArrAtoB($r)\n{\n    $r[\'a\'] = $r[\'b\'];\n}\n\n//-------------------------------------\n\nfunction glb_cls()\n{\n    global $xo;\n\n    $xo-&gt;d = "ddxxdd";\n    return ($xo-&gt;f);\n}\n\n//-------------------------------------\n\nfunction glb_arr()\n{\n    global $xr;\n\n    $xr[\'d\'] = "ddxxdd";\n    return ($xr[\'f\']);\n}\n\n//-------------------------------------\n\n?&gt;\n</code></pre>\n\n<p>test reads: 1\ntest assignments: 1\ntest globals: 1\ntest functions: 1</p>\n\n<p>Array time = 1.58905816078\nClass time = 1.11980104446\nTotal Array time (so far) = 1.58903813362(100,000 iterations)\nTotal Class time (so far) = 1.11979603767(100,000 iterations)</p>\n\n<p>Array time = 1.02581000328\nClass time = 1.22492313385\nTotal Array time (so far) = 2.61484408379(100,000 iterations)\nTotal Class time (so far) = 2.34471416473(100,000 iterations)</p>\n\n<p>Array time = 1.29942297935\nClass time = 1.18844485283\nTotal Array time (so far) = 3.91425895691(100,000 iterations)\nTotal Class time (so far) = 3.5331492424(100,000 iterations)</p>\n\n<p>Array time = 1.28776097298\nClass time = 1.02383089066\nTotal Array time (so far) = 5.2020149231(100,000 iterations)\nTotal Class time (so far) = 4.55697512627(100,000 iterations)</p>\n\n<p>Array time = 1.31235599518\nClass time = 1.38880181313\nTotal Array time (so far) = 6.51436591148(100,000 iterations)\nTotal Class time (so far) = 5.94577097893(100,000 iterations)</p>\n\n<p>Array time = 1.3007349968\nClass time = 1.07644081116\nTotal Array time (so far) = 7.81509685516(100,000 iterations)\nTotal Class time (so far) = 7.02220678329(100,000 iterations)</p>\n\n<p>Array time = 1.12752890587\nClass time = 1.07106018066\nTotal Array time (so far) = 8.94262075424(100,000 iterations)\nTotal Class time (so far) = 8.09326195717(100,000 iterations)</p>\n\n<p>Array time = 1.08890199661\nClass time = 1.09139609337\nTotal Array time (so far) = 10.0315177441(100,000 iterations)\nTotal Class time (so far) = 9.18465089798(100,000 iterations)</p>\n\n<p>Array time = 1.6172170639\nClass time = 1.14714384079\nTotal Array time (so far) = 11.6487307549(100,000 iterations)\nTotal Class time (so far) = 10.3317887783(100,000 iterations)</p>\n\n<p>Array time = 1.53738498688\nClass time = 1.28127002716\nTotal Array time (so far) = 13.1861097813(100,000 iterations)\nTotal Class time (so far) = 11.6130547523(100,000 iterations) </p>\n\n<p>TOTAL TIMES:\nTotal Array time = 13.1861097813(1,000,000 iterations)\nTotal Class time = 11.6130547523(1,000,000 iterations) </p>\n\n<p>So, either way the difference is pretty negligible.  I was very suprized to find that once you start accessing things globally, classes actually become a little faster.</p>\n\n<p>But don\'t trust me, run it for your self.  I personally now feel completely guilt free about using classes in my high performance applications. :D</p>\n'}, {'answer_id': 13213350, 'author': 'rtconner', 'author_id': 587388, 'author_profile': 'https://Stackoverflow.com/users/587388', 'pm_score': 0, 'selected': False, 'text': '<p>@Richard Varno</p>\n\n<p>I Ran your exact code (after fixed the small bugs), and got much different results than you. Classes ran much on my PHP 5.3.17 install.</p>\n\n<p>Array time = 0.69054913520813\nClass time = 1.1762700080872</p>\n\n<p>Total Array time (so far) = 0.69054508209229(100,000 iterations)\nTotal Class time (so far) = 1.1762590408325(100,000 iterations)</p>\n\n<p>Array time = 0.99001502990723\nClass time = 1.22034907341</p>\n\n<p>Total Array time (so far) = 1.6805560588837(100,000 iterations)\nTotal Class time (so far) = 2.3966031074524(100,000 iterations)</p>\n\n<p>Array time = 0.99191808700562\nClass time = 1.2245700359344</p>\n\n<p>Total Array time (so far) = 2.6724660396576(100,000 iterations)\nTotal Class time (so far) = 3.6211669445038(100,000 iterations)</p>\n\n<p>Array time = 0.9890251159668\nClass time = 1.2246470451355</p>\n\n<p>Total Array time (so far) = 3.661484003067(100,000 iterations)\nTotal Class time (so far) = 4.8458080291748(100,000 iterations)</p>\n\n<p>Array time = 0.99573588371277\nClass time = 1.1242771148682</p>\n\n<p>Total Array time (so far) = 4.6572148799896(100,000 iterations)\nTotal Class time (so far) = 5.9700801372528(100,000 iterations)</p>\n\n<p>Array time = 0.88518786430359\nClass time = 1.1427340507507</p>\n\n<p>Total Array time (so far) = 5.5423986911774(100,000 iterations)\nTotal Class time (so far) = 7.1128082275391(100,000 iterations)</p>\n\n<p>Array time = 0.87605404853821\nClass time = 0.95899105072021</p>\n\n<p>Total Array time (so far) = 6.4184486865997(100,000 iterations)\nTotal Class time (so far) = 8.0717933177948(100,000 iterations)</p>\n\n<p>Array time = 0.73414516448975\nClass time = 1.0223190784454</p>\n\n<p>Total Array time (so far) = 7.1525888442993(100,000 iterations)\nTotal Class time (so far) = 9.0941033363342(100,000 iterations)</p>\n\n<p>Array time = 0.95230412483215\nClass time = 1.059828042984</p>\n\n<p>Total Array time (so far) = 8.1048839092255(100,000 iterations)\nTotal Class time (so far) = 10.153927326202(100,000 iterations)</p>\n\n<p>Array time = 0.75814390182495\nClass time = 0.84455919265747</p>\n\n<p>Total Array time (so far) = 8.8630249500275(100,000 iterations)\nTotal Class time (so far) = 10.998482465744(100,000 iterations)\nTOTAL TIMES:</p>\n\n<p>Total Array time = 8.8630249500275(1,000,000 iterations)\nTotal Class time = 10.998482465744(1,000,000 iterations) </p>\n'}]
['https://Stackoverflow.com/questions/24130', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1384652/']