Classes vs 2D Arrays

What is better to use in PHP, a 2D array or a class? I have included an example of what I mean.

// 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 someone does not exit and shows that the classes are too slow, it seems that the class wins.

I don’t know which answer I should accept, I just supported them all.


And now I wrote two almost identical pages, one of which uses a 2D array (written before this question was published), and now one of them uses a class, and I have to say that the class produces much more pleasant code. I do not know how much overhead will be created, but I doubt that this will compete with the improvement of the code itself.

Thanks for helping make me a better programmer.

+6
arrays php class
source share
9 answers

The "class" you created above is that most people will use struct for other languages. I'm not sure what the performance implications are in PHP, although I suspect that instantiating objects is likely to be more expensive here, at least a little.

If we say that if the cost is relatively low, then, in my opinion, it is a little easier to manage objects.

I am only saying the following based on the title and your question, but: Keep in mind that classes provide the advantage of methods and access control. Therefore, if you want people to not change weights to negative numbers, you can make the weight field private and provide some access methods, for example getWeight() and setWeight() . Inside setWeight() you can do some validation of values, for example:

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

It depends on what you mean by "better." I would go in an object-oriented way (using classes) because I believe that it makes cleaner code (at least in my opinion). However, I'm not sure what speed penalties may be for this option.

+4
source share

As a rule, I follow this rule:

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

2) Make it a 2D array if you use it to quickly process data in one part of your application.

+4
source share

This is the speed that I think of mainly for something more complicated than what I have here, I would probably go with classes, but the question is, what is the cost of the class?

It would seem that this is premature optimization. Your application will not take any real performance effect in any way, but using the class allows you to use getter and setter methods and, as a rule, it will be better for encapsulating the code and reusing the code.

With the arrays that you spend more difficult to read and maintain, you cannot unit test the code as easily and with a good class structure, it should be easier for other developers to understand if they need to accept it.

And when you need to add other methods to manage them, you will not have an extension of the architecture.

+3
source share

The class you have is not a real class in terms of OO - it just managed to capture the space of instance variables.

That being said - there may not be many speed issues there - it's just the style in your example.

The tricky bit is if you decide that the object is a real "human" class, and think about other attributes and actions that you might wish for a human class, then you will notice not only the performance of the style - writing code, but also speed of work.

+2
source share

If your code uses many functions that work with these attributes (name / height / weight), then using a class can be a good option.

0
source share

Teifion, if you use classes as a simple replacement for arrays, you are nowhere near OOP. The essence of OOP is that objects have knowledge and responsibility, can actually do something and collaborate with other classes. Your objects only have knowledge and cannot do anything other than the fact that they exist, but they seem to be good candidates for persistence providers (objects that know how to store / retrieve themselves to / from the database).

Do not worry about performance. Objects in PHP are quick and easy, and overall performance is greatly overestimated. It is cheaper to save your time as a programmer using the right approach than saving microseconds in your program with some obscure, it is difficult to debug and fix a piece of code.

0
source share

Most tests that temporary arrays against classes only test them. As soon as you start to do something with them.

I was a purist who only used arrays because the performance was much better. I wrote the following code to justify myself, to justify the extra difficulties of using classes (even if they are simpler than a programmer)

Let's say I was VERY surprised by the results!

  <?php $rx = ""; $rt = ""; $rf = ""; $ta = 0; // total array time $tc = 0; // total class time // flip these to test different attributes $test_globals = true; $test_functions = true; $test_assignments = true; $test_reads = true; // define class class TestObject { public $a; public $b; public $c; public $d; public $e; public $f; public function __construct($a,$b,$c,$d,$e,$f) { $this->a = $a; $this->b = $b; $this->c = $c; $this->d = $d; $this->e = $e; $this->f = $f; } public function setAtoB() { $this->a = $this->b; } } // begin test echo "<br>test reads: " . $test_reads; echo "<br>test assignments: " . $test_assignments; echo "<br>test globals: " . $test_globals; echo "<br>test functions: " . $test_functions; echo "<br>"; for ($z=0;$z<10;$z++) { $starta = microtime(true); for ($x=0;$x<100000;$x++) { $xr = getArray('aaa','bbb','ccccccccc','ddddddddd','eeeeeeee','fffffffffff'); if ($test_assignments) { $xr['e'] = "e"; $xr['c'] = "sea biscut"; } if ($test_reads) { $rt = $x['b']; $rx = $x['f']; } if ($test_functions) { setArrAtoB($xr); } if ($test_globals) { $rf = glb_arr(); } } $ta = $ta + (microtime(true)-$starta); echo "<br/>Array time = " . (microtime(true)-$starta) . "\n\n"; $startc = microtime(true); for ($x=0;$x<100000;$x++) { $xo = new TestObject('aaa','bbb','ccccccccc','ddddddddd','eeeeeeee','fffffffffff'); if ($test_assignments) { $xo->e = "e"; $xo->c = "sea biscut"; } if ($test_reads) { $rt = $xo->b; $rx = $xo->f; } if ($test_functions) { $xo->setAtoB(); } if ($test_globals) { $xf = glb_cls(); } } $tc = $tc + (microtime(true)-$startc); echo "<br>Class time = " . (microtime(true)-$startc) . "\n\n"; echo "<br>"; echo "<br>Total Array time (so far) = " . $ta . "(100,000 iterations) \n\n"; echo "<br>Total Class time (so far) = " . $tc . "(100,000 iterations) \n\n"; echo "<br>"; } echo "TOTAL TIMES:"; echo "<br>"; echo "<br>Total Array time = " . $ta . "(1,000,000 iterations) \n\n"; echo "<br>Total Class time = " . $tc . "(1,000,000 iterations)\n\n"; // test functions function getArray($a,$b,$c,$d,$e,$f) { $arr = array(); $arr['a'] = $a; $arr['b'] = $b; $arr['c'] = $c; $arr['d'] = $d; $arr['d'] = $e; $arr['d'] = $f; return($arr); } //------------------------------------- function setArrAtoB($r) { $r['a'] = $r['b']; } //------------------------------------- function glb_cls() { global $xo; $xo->d = "ddxxdd"; return ($xo->f); } //------------------------------------- function glb_arr() { global $xr; $xr['d'] = "ddxxdd"; return ($xr['f']); } //------------------------------------- ?> 

test reads: 1 test items: 1 test global values: 1 test functions: 1

Array time = 1.58905816078 Time class = 1.11980104446 Total array time (so far) = 1.58903813362 (100,000 iterations) Total class time (still) = 1.11979603767 (100,000 iterations)

Array time = 1.02581000328 Time class = 1.22492313385 Total array time (so far) = 2.61484408379 (100,000 iterations) Total class time (still) = 2.34471416473 (100,000 iterations)

Array time = 1.29942297935 Time class = 1.18844485283 Total array time (so far) = 3.91425895691 (100,000 iterations) Total class time (still) = 3,5331492424 (100,000 iterations)

Array time = 1.28776097298 Time class = 1.02383089066 Total array time (for now) = 5.2020149231 (100,000 iterations) Total class time (still) = 4.55697512627 (100,000 iterations)

Array time = 1.31235599518 Occupation time = 1.38880181313 Total array time (still) = 6.51436591148 (100,000 iterations) Total class time (still) = 5.94577097893 (100,000 iterations)

Array time = 1.3007349968 Time class = 1.07644081116 Total array time (so far) = 7.81509685516 (100,000 iterations) Total class time (still) = 7.02220678329 (100,000 iterations)

Array time = 1.12752890587 Time class = 1.07106018066 Total array time (still) = 8.94262075424 (100,000 iterations) Total class time (still) = 8.09326195717 (100,000 iterations)

Array time = 1.08890199661 Time class = 1.09139609337 Total array time (so far) = 10.0315177441 (100,000 iterations) Total class time (still) = 9.18465089798 (100,000 iterations)

Array time = 1.6172170639 Class time = 1.14714384079 Total array time (so far) = 11.6487307549 (100,000 iterations) Total class time (still) = 10.3317887783 (100,000 iterations)

Array time = 1.53738498688 Class time = 1.28127002716 Total array time (until now) = 13.1861097813 (100,000 iterations) Total class time (still) = 11.6130547523 (100,000 iterations)

TOTAL TIME: Total array time = 13.1861097813 (1,000,000 iterations) Total Class time = 11.6130547523 (1,000,000 iterations)

So, in any case, the difference is pretty slight. I was very surprised to find that as soon as you start accessing things all over the world, classes will really get a little faster.

But do not believe me, run it for yourself. I personally now feel completely guilty of using classes in my high-performance applications .: D

0
source share

@ Richard Varno

I installed your exact code (after fixing small errors) and got different results than you. The classes worked a lot on my PHP 5.3.17 installation.

Array time = 0.69054913520813 Class time = 1.1762700080872

Total array time (still) = 0.69054508209229 (100,000 iterations) Total class time (still) = 1,1762590408325 (100,000 iterations)

Array time = 0.99001502990723 Time class = 1.22034907341

Total array time (still) = 1,6805560588837 (100,000 iterations) Total class time (still) = 2.3966031074524 (100,000 iterations)

Array time = 0.99191808700562 Class time = 1.2245700359344

Total array time (still) = 2.6724660396576 (100,000 iterations) Total class time (still) = 3.6211669445038 (100,000 iterations)

Array time = 0.9890251159668 Class time = 1.2246470451355

Total array time (still) = 3.661484003067 (100,000 iterations) Total class time (still) = 4.8458080291748 (100,000 iterations)

Array time = 0.99573588371277 Class time = 1.1242771148682

Total array time (still) = 4.6572148799896 (100,000 iterations) Total class time (still) = 5.9700801372528 (100,000 iterations)

Array time = 0.88518786430359 Class time = 1.1427340507507

Total array time (still) = 5.5423986911774 (100,000 iterations) Total class time (still) = 7.1128082275391 (100,000 iterations)

Array time = 0.87605404853821 Class time = 0.95899105072021

Total array time (still) = 6.4184486865997 (100,000 iterations) Total class time (still) = 8.0717933177948 (100,000 iterations)

Array time = 0.73414516448975 Time class = 1.0223190784454

Total array time (still) = 7.1525888442993 (100,000 iterations) Total class time (still) = 9.0941033363342 (100,000 iterations)

Array time = 0.95230412483215 Time class = 1.059828042984

Total array time (still) = 8.1048839092255 (100,000 iterations) Total class time (still) = 10.153927326202 (100,000 iterations)

Array time = 0.75814390182495 Class time = 0.84455919265747

Total array time (still) = 8.8630249500275 (100,000 iterations) Total class time (still) = 10.998482465744 (100,000 iterations) TOTAL TIMES:

Total array time = 8.8630249500275 (1,000,000 iterations) Total class time = 10.998482465744 (1,000,000 iterations)

0
source share

All Articles