[wp-trac] [WordPress Trac] #51525: Add new functions apply_filters_single_type() and apply_filters_ref_array_single_type()
WordPress Trac
noreply at wordpress.org
Tue Oct 20 15:25:41 UTC 2020
#51525: Add new functions apply_filters_single_type() and
apply_filters_ref_array_single_type()
------------------------------------------+---------------------
Reporter: jrf | Owner: (none)
Type: feature request | Status: new
Priority: normal | Milestone: 5.6
Component: General | Version: trunk
Severity: normal | Resolution:
Keywords: 2nd-opinion needs-patch php8 | Focuses:
------------------------------------------+---------------------
Comment (by overclokk):
I would like to add some sugar to this ticket because I'd like to have
more "typed code".
I thought at two alternative:
1 - Have as many functions as many type we want to support, one function
per type:
{{{#!php
<?php
\apply_filters_type_string( 'hook', 'string', ...$args );
\apply_filters_type_int( 'hook', 1, ...$args );
\apply_filters_type_array( 'hook', ['val1', 'val2'], ...$args );
// ... and so on
}}}
Why? Because I think of having more declarative function reduce the amount
of logic inside the function itself, separation of concern, and I don't
see any issue with having more API functions in core if they can help to
write better code, and we don't have to write functions for all type, only
for some and let the developers to write its own if they need them.
With this we could simply switch from `\apply_filters()` to
`\apply_filters_type_*()` simpler.
2 - A system more similar to PSR-14 and only for one type, `object`:
{{{#!php
<?php
function do_action_psr( 'hook', object $obj ) {
// Do some stuff
}
//...
$obj = new My_Object( /* With arguments */ );
\do_action_psr( 'hook', $obj );
}}}
Why? Because with this I could use the `Value Object` pattern and if I
want to do stuff more strictly I can write a decorator function like this:
{{{#!php
<?php
function do_action_my_interface( 'hook', My_Interface $obj ) {
// Do some stuff
\do_action_psr( 'hook', object $obj );
}
//...
$obj = new My_Object( /* With arguments */ ) // extends My_Interface;
\do_action_my_interface( 'hook', object $obj );
}}}
With objects I don't even need to use `apply_filters()` because they are
passed by reference.
Beetween the two I prefer the second even if I can see that there's one
issue with this, if we want more types we have to write more decorators.
Obviously these are only my two cents.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/51525#comment:9>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list