4

I have always fetched database results as Arrays and NOT Objects and the reason being PHP provides so many functions to manipulate arrays that it makes my life easy.

I went over this SO Question Objects or Arrays?

But it does not answer my question. Guy who worked before in my project always fetched results as Objects and it gave me a real hard time converting it to arrays and then manipulate them since I was generating leads statistics. Array Functions made it easy.

Is there an easy way to do so? or Any other Alternative? Why go for Objects and not Arrays?

NOTE: I am not considering performance but my ease of manipulation.

Community
  • 1
  • 1
Abhinav
  • 8,028
  • 12
  • 48
  • 89
  • 1
    Maybe try to convert the objects from your results into [`ArrayObjects`](http://php.net/manual/en/arrayobject.construct.php) and then to use: [`getArrayCopy()`](http://php.net/manual/en/arrayobject.getarraycopy.php) to convert it into an array (Maybe you don't even have to convert it to an array, if the arrayObject has the functionality which you want: http://php.net/manual/en/class.arrayobject.php). Or you can also try to simply cast them, e.g. `$array = (array) $myObject;` – Rizier123 May 17 '15 at 10:04
  • I was unaware of the ArrayObjects. And yes when i used casting, it dint work for the multidimensional objects, just the elements in the first level turned to an Array not the datas of the element,it was still the object, then I had to use foreach loop converting them to arrays – Abhinav May 17 '15 at 10:11
  • I will be trying out ArrayObjects now...thanx for your suggestion but I still just dont get it why ppl go for Objects! – Abhinav May 17 '15 at 10:12
  • imo. It can be useful to use objects, if the data returned from the query represents the 'state vector' of a custom object, as you then get access to all the 'methods' to manipulate the data. Another advantage is 'property access' which can read more nicely in code. The `ArrayObject` class is very useful as it allows 'property access' as well as 'index access'. – Ryan Vincent May 17 '15 at 11:38
  • One reason to pick objects over arrays is that your code can then say "this result row is represented by a `Visit` object, which has fields `$id`, `$ip_address`, `$visit_time`, etc. ... ", and then you don't have to read the query (which might be ten files away, dynamically generated, or a hundred lines long) to know what fields you should be using. And you can use [static analysis tools](https://www.jetbrains.com/phpstorm/) to [validate your code while you write it](https://www.jetbrains.com/img/webhelp/idea/undefined_field.png). – DCoder May 17 '15 at 18:02

1 Answers1

4

In PHP there is absolutely zero important advantage of returning an instance of stdClass object over a plain array.

And you got the point, if you return an array you can use the moltitude of php function array_* to manipulate the data, and that is a big advantage.

Of coure in either case you can convert back and forth to object and array by simply cast it:

$array = (array) $object;
$object = (object) $array;

You are given the possibility to choose to get objects because it is a common pratice to use an ORM while quering the database. (Mandatory citation: ORM vietnam)

Note that using PDO you can return an object that you specify based on your query, example:

$stmt = $dbh->query("SELECT * FROM animals");
$stmt->setFetchMode(PDO::FETCH_INTO, new animals);

But at this point you simply should use an ORM, if you need objects

dynamic
  • 46,985
  • 55
  • 154
  • 231
  • sorry..I could not quite understand it..can u give me a layman explanation? I dont use ORM. Active records of Code Igniter is what I use. So u mean to say that Objects should be used when ORM is used? – Abhinav May 18 '15 at 10:39
  • Active Record is an ORM pattern. To better understand the differences try reading here: http://culttt.com/2014/06/18/whats-difference-active-record-data-mapper/ To answer your second question, yes: when using ORM you are manipulating objects – dynamic May 18 '15 at 10:50
  • Okay I got it and so whenever I need to manipulate it I can always convert them to arrays and do my stuff! Thanx – Abhinav May 18 '15 at 10:55