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.

5
  • 1
    Maybe try to convert the objects from your results into ArrayObjects and then to use: getArrayCopy() 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: php.net/manual/en/class.arrayobject.php). Or you can also try to simply cast them, e.g. $array = (array) $myObject; Commented May 17, 2015 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 Commented May 17, 2015 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! Commented May 17, 2015 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'. Commented May 17, 2015 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 to validate your code while you write it. Commented May 17, 2015 at 18:02

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

3 Comments

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?
Active Record is an ORM pattern. To better understand the differences try reading here: culttt.com/2014/06/18/… To answer your second question, yes: when using ORM you are manipulating objects
Okay I got it and so whenever I need to manipulate it I can always convert them to arrays and do my stuff! Thanx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.