Opened 7 years ago
Last modified 7 years ago
#49032 new feature request
Introduce __return_value()
| Reported by: | giuseppe.mazzapica | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | General | Version: | |
| Severity: | normal | Keywords: | |
| Cc: | Focuses: |
Description
WordPress has, since quite long time, helper functions like __return_true and others.
These are useful to be added to filters.
Now that minimum PHP version is 5.6 we could have something like:
<?php function __return_value($value) { return static function () use ($value) { return $value; } } function __return_value_by_ref($value) { return static function () use (&$value) { return $value; } }
which would quite useful in many places, at least as useful as the other mentioned functions that are already there.
Change History (2)
#2
in reply to: ↑ 1
@
7 years ago
Replying to Rarst:
Related on removing complex callbacks #46635
Ok. Don't make much sense to me in this case because if someone wants a filter to return a constant value, they will use a closure anyway. But I don't expect consensus in core to make sense to me.
I guess in a few months when I'll move everything to PHP 7.4 I'll just use short closures and forget about this.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
I think the current core consensus is against using closure callbacks in core.
Related on removing complex callbacks #46635
For trivia I implemented hooking a concrete return value a long time ago and it never got much interest https://github.com/Rarst/advanced-hooks-api