Opened 10 years ago
Closed 10 years ago
#35346 closed defect (bug) (wontfix)
WP_Term breaks type-hints for stdClass
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Milestone: | Priority: | normal | |
| Severity: | normal | Version: | 4.4 |
| Component: | Taxonomy | Keywords: | close |
| Focuses: | Cc: |
Description
Since 4.4.0 instances of WP_Term is returned from various function calls in WordPress core. Before the return value(s) were instances of stdClass. #14162
This makes code that type-hints stdClass not work with 4.4.0+, if you for instance have a callback for array_map() that expects a stdClass you will get:
Argument 1 passed to {closure}() must be an instance of stdClass, instance of WP_Term given
This would be easily fixed by just making WP_Term extend stdClass like this:
final class WP_Term extends stdClass { }
I haven't tested if this breaks something else, but would you consider this change, or is it up to the plugin/theme developer to change all code that expects a stdClass to be able to support 4.4.0+?
Change History (4)
This ticket was mentioned in Slack in #core by dd32. View the logs.
10 years ago
#3
@
10 years ago
I agree with @johnbillion here. In general I don't think client code should be typehinting stdClass because that's really brittle. stdClass is like a placeholder for better things. So I also favor a wontfix for this ticket.
#4
@
10 years ago
- Milestone Awaiting Review deleted
- Resolution set to wontfix
- Status changed from new to closed
Thanks for the ticket, @exz . The consensus here and in Slack seems to be that type-hinting for stdClass is not something we want to support. Documentation has always noted that an object (not a stdClass) would be returned here.
I don't think it's unreasonable to expect that a
stdClasspseudo-object will at some point get upgraded to a proper class, as happened withWP_Termin 4.4 and withWP_User,WP_Post, etc in previous releases.This makes type hinting
stdClassfragile. I think this issue is a wontfix.