Welcome Guest, Not a member yet? Register   Sign In
result() vs. result_array() ?
#1

Usually i like to get my result as object thats why i use result mostly.

But i want to know which is the better approach that i should follow, Which one of them is more efficient to use in regards to performance?

Here is the Example i am talking about:

Code:
$query = $this->db->get();
$result = $query->result();

or is this should be the better??

Code:
$query = $this->db->get();
$result = $query->result_array();

Thank you.
Reply
#2

It doesn't matter.
Reply
#3

Both are good however when you use result_array() it makes the query and places it in an array and when you use result() it puts the query in a stdClass object there are these two forms because you can not return a stdClass object in a method only in the array. Sorry my english poor!
Reply
#4

Doesn't matter, it's up to developers preference on working with the data. I prefer array's as merging is easier but a lot of dev's prefer objects for their syntax.
Reply
#5

as others have said, it doesn't matter, it depends on personal choice and personal taste of syntax...
however if you are merging your results then you should prefer result_array(), but like me if you are using ci's inbuilt template parser then you should prefer result()..
Reply
#6

Yes, it's mostly a matter of personal preference.
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
Reply
#7

thanks guys if It doesn't matter. right now i will still using result() in my generic model.
Reply
#8

For all practical reasons I agree with everyone else - it doesn't matter. There is a very slight performance edge using objects, since they don't require the extra memory allocation that an array uses, but objects are much less flexible. And optimizing this at the point of your development that any of use probably in? A waste of time. Smile

Me - I have a tendency to use objects when I can, simply because I prefer the syntax. The more I work with complex applications, though, the more I standardize on arrays since there are so many useful functions in PHP for arrays.
Reply
#9

What about when passed as parameters on a function call? What is the best practice for implementing the function?

function doSomething($data_array)

or

function doSomething($object)?
Reply
#10

I'd also say that's a matter of personal preference.
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB