Opened 5 years ago
Closed 12 months ago
#44579 closed defect (bug) (fixed)
get_option() is type aware now (returns Integers as Integers)
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | 5.8 | Priority: | normal |
Severity: | minor | Version: | |
Component: | Options, Meta APIs | Keywords: | |
Focuses: | docs | Cc: |
Description
Hi,
I noticed that the get_option() function now returns the value type it was originally given (ex. I give an INT in update_option(), then get_option() returns an INT).
But the doc doesn't seems to agree with that:
wp-includes/option.php:
* Any scalar values will be returned as strings.
This text seems to be there for historical reasons (https://core.trac.wordpress.org/ticket/31820) and something changed in the code since then, but I can't find the related commit.
Proposed text to fix the doc:
* This function is type-aware: the return type will be the same as the last given input value.
Thx!
Change History (5)
#3
@
5 years ago
Thanks for the insight @boonebgorges :)
I did some more unit testing with $wp_object_cache->flush() to simulate the page reload and indeed, I can confirm the values are then returned as strings.
It's a good idea to update the documentation in order to reflect this behavior... but on the other hand, I'm a little disturbed by said behavior...
Wouldn't it be better to have a consistent return type, whatever the use case? For reliability purpose?
Regards
#5
@
12 months ago
- Milestone changed from Awaiting Review to 5.8
- Resolution set to fixed
- Status changed from new to closed
Replying to boonebgorges:
This being said, this bit of documentation could probably be improved. Something like "Note that scalar values may be returned as strings when retrieved from the database" might be a more accurate wording.
It looks like the documentation was updated for WordPress 5.8 in [51050] / #51278. I believe this ticket is now resolved.
Hi @gdespoulain - Welcome to Trac, and thanks for the ticket!
The bit of documentation you cite here was added in [36234]. You can read the backstory in #31820 and #22192, but briefly:
get_option()
and similar functions return all values as strings *except when pulled from memory on the same pageload as whenupdate_option()
is called*. So:You may also see the same behavior between pageloads if you're running a persistent cache (Memcache, Redis, etc) depending on the nature of the cache. Can you confirm?
This being said, this bit of documentation could probably be improved. Something like "Note that scalar values may be returned as strings when retrieved from the database" might be a more accurate wording.