3

In what case is it more applicable to use:

$Obj_Array = new ArrayObject(array(), ArrayObject::STD_PROP_LIST);
 $Obj_Array->Key = "Value";

Rather:

 $Array = array(); 
 $Array['Key'] = "Value"; 

Now, these are both different. I can tell that far, even know after performing research I see no real reason to have a preference on object arrays over traditional arrays..

So Could someone Show me an active example on how object arrays provide more benefit over a normal array


In addition, i'm aware that these object arrays are fairly new to PHP.. But nearly all database functions for example, returns either an array or single variables:

$Query = $DB->prepare("SELECT * FROM TBL WHERE col=?");
$Query->bind_param('s',$Variable); 
$Query->execute();
$Query->bind_result($Col1, $Col2, $Col3);
$Query->fetch();
$Query->close(); 

The above example returns the contents as singe variables.

So to create an object array using a while loop:

$Obj_Array = new ArrayObject(array(), ArrayObject::STD_PROP_LIST);
$Key_Creation = 0;   

$Query = $DB->prepare("SELECT Username,Password FROM TBL WHERE col=?");
$Query->bind_param('s',$Variable); 
$Query->execute();
$Query->bind_result($Col1, $Col2);
while($Query->fetch()){
  $Obj_Array->$Key_Creation = array($Col1,$Col2);
  $Key_Creation++;
}
$Query->close();
Daryl Gill
  • 5,464
  • 9
  • 36
  • 69

2 Answers2

2

ArrayObject is useful when you have to make code that operates on an object and code that operates on an array work together, assuming that it is impossible or undesirable to change one part of the code to behave as the other one does.

In practice, if you are in control of one or both parts of the code you do not have any need for ArrayObject and using it gains you nothing other than reduced performance.

My personal opinion is that ArrayObject should be avoided like the plague, just another one in the long list of half-baked "features" of dubious usefulness in PHP.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • I have thrown on what I have been thinking about objarrs, is there anything you could comment on that? – Daryl Gill May 18 '13 at 23:15
  • @DarylGill: I 'm not sure what you mean. – Jon May 18 '13 at 23:21
  • I understand this may be a preference of style, but it seems to be a long winded process compared to traditional arrays – Daryl Gill May 18 '13 at 23:25
  • @DarylGill: It's even worse than that. If all you want is syntactic sugar and/or built-in reference semantics you can simply use a `new stdClass` and store properties in there. `ArrayObject` provides these too, and it saves you writing some code if you want to implement a couple of more advanced scenarios -- that's about it. But it also forces you to use array notation, while at the same time it does not work with array functions or with functions that accept a type-hinted array argument. I don't know how else to say it, but IMO it's dangerously close to useless crap. – Jon May 18 '13 at 23:30
  • Aah, I get what your saying. if I want to work with objects, create a class. If I want to work with arrays, do not mix the two items into one. Keep them seperate? – Daryl Gill May 18 '13 at 23:33
  • @DarylGill: Close enough. :-) – Jon May 18 '13 at 23:34
1

It is a matter of preferred style. The new ArrayObjects which was introduces to PHP 5 is the Object oriented edition of arrays. Many programmers prefer the more OO style of programming and the old arrays doesn't fit into the OO world.

There might be a small performance penalty to ArrayObjects, but in php 5.3 this is nearly gone.

CodeTower
  • 6,293
  • 5
  • 30
  • 54
  • The object oriented edition of arrays is `stdClass`. The only thing `ArrayObject` offers on top of that is a couple of trinkets for the savages, for example ready-made implementations for reordering and counting the values. – Jon May 18 '13 at 23:18
  • You can't to the best of my knowledge add unnamed values to a stdClass. – CodeTower May 19 '13 at 08:22