Jump to content

Returning PDO query by default as array or objects


NotionCommotion

Recommended Posts

Originally, I would get both, and unfortunately would inconsistently use both.  Then, I wanted more consistently, so configured php.ini to only return objects as I felt accessing them was more concise.  And then later, I found myself needing arrays more often, and initially would just typecast them to arrays but eventually my standard was to set the PDO's fetch style to an array.  And now I find myself almost always explicitly requesting arrays and occasionally requesting columns or named values.

Do you configure ATTR_DEFAULT_FETCH_MODE, and if so to what?

PS.  Anyone use FETCH_CLASS, FETCH_INTO or FETCH_NUM?  If so, what would be a good use case?

Link to comment
Share on other sites

I actually tend to use BOTH. It won't* double the amount of memory used because PHP is nice like that, and it allows constructs like

foreach (/* SELECT column1, column2 FROM table */ AS list($column1, $column2)) {
	// ...
}

without having to override the preference for a particular query. That said, PHP 7.1 allows

foreach (/* SELECT column1, column2 FROM table */ AS list("column1" => $column1, "column2" => $column2)) {
	// ...
}

so the BOTH/NUM advantage kinda goes out the window... but I'm still writing for 7.0 so I can't use that yet.

As for OBJ, I'd rather work with simple arrays than anonymous objects. (I also always json_decode to associative arrays instead of objects.)

Of course there are the other options, but
- BOUND and INTO aren't reasonable as defaults
- CLASS would only work as a default if you mapped everything into some generic "database row" class, and there's no autocomplete IDE support for it
- LAZY is a nice convenience but I wouldn't use it because mixing array and object syntax is weird
- NAMED is something so specialized that you should have to set it explicitly for self-documentation purposes

* I assume - I've never actually verified this, but PHP definitely should be sharing the values in its normal copy-on-write style.

Link to comment
Share on other sites

 

22 hours ago, requinix said:

I actually tend to use BOTH. It won't* double the amount of memory used because PHP is nice like that, and it allows constructs like

Good point.  As seen again, using the default settings are usually the way to go unless one has a good reason to do differently.

Related, do you explicitly set any default values such as PDO:FETCH_BOTH?  The reason to do so is "if" PHP changes, the application will not break, and it clearly communicates which mode is being used.  But cluttering script with a bunch of redundant settings will cause more trouble than it is worth.  Never mind, answered my question.

There is a much about fetch modes which I did not know, and I recently stumbled upon https://phpdelusions.net/pdo/fetch_modes#FETCH_GROUP

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.