NotionCommotion Posted January 17, 2019 Share Posted January 17, 2019 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? Quote Link to comment Share on other sites More sharing options...
Barand Posted January 17, 2019 Share Posted January 17, 2019 My default is PDO::FETCH_ASSOC I have oocasionally (a search found 2) overridden that with fetch(PDO::FETCH_NUM) EG list() requires a numeric index, array_merge() works differently with associative and numeric indexes and that "..." variadic thing needs a numeric index. Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted January 17, 2019 Author Share Posted January 17, 2019 Ah, that's right, there are some good uses for FETCH_NUM. After more thought, FETCH_CLASS will come in handy, but by no means should be a default. I plan on makding FETCH_ASSOC my default as well. Thanks Quote Link to comment Share on other sites More sharing options...
requinix Posted January 18, 2019 Share Posted January 18, 2019 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. Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted January 19, 2019 Author Share Posted January 19, 2019 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.