WordPress.org

Make WordPress Core

Opened 10 months ago

Last modified 10 months ago

#49032 new feature request

Introduce __return_value()

Reported by: giuseppe.mazzapica Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: General Keywords:
Focuses: Cc:

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)

#1 follow-up: @Rarst
10 months ago

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

#2 in reply to: ↑ 1 @giuseppe.mazzapica
10 months 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.

Note: See TracTickets for help on using tickets.