Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

When returning things from the database, by default Codeigniter methods return objects (i.e. $this->db->result() vs $this->db->result_array(), and the same for row/row_array).

I'm a developer by sheer interest, not education, so excuse me if this is a stupid question, but what does it matter which it returns, and why does the preference - based on the naming convention - seem to prefer objects over arrays, when from what I can tell php developers love arrays?

I did some background reading, and it seems like with PHP 5+, the execution difference on arrays vs objects is negligible. So far the only difference I've come across is -> as opposed to [].

This question likely isn't specific to Codeigniter, but that's my frame of reference.

share|improve this question

1 Answer 1

I'm not sure the history behind why the api got this way, but if you fetch objects you can make CI db lib instantiate custom classes with the data, like the various native PHP *_fetch_object() do.

 $query = $this->db->query("SELECT * FROM users;");
 foreach ($query->result('User') as $row) {
     // $row is not an instance of User
 }

The default stdObjects are not too useful though, in my opinion ArrayObject would be a better solution since those support both -> and [] access, but CI come from the PHP4 era when those were not around.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.