[wp-trac] [WordPress Trac] #50095: WP_List_Util::filter() not compatible with lists of objects that use magic methods

WordPress Trac noreply at wordpress.org
Wed May 6 01:38:10 UTC 2020


#50095: WP_List_Util::filter() not compatible with lists of objects that use magic
methods
-----------------------------+-----------------------------
 Reporter:  johnjamesjacoby  |      Owner:  (none)
     Type:  defect (bug)     |     Status:  new
 Priority:  normal           |  Milestone:  Awaiting Review
Component:  Formatting       |    Version:  4.7
 Severity:  normal           |   Keywords:
  Focuses:                   |
-----------------------------+-----------------------------
 When calling `wp_filter_object_list()` and passing in an array of objects
 that use magic methods (specifically `__isset()` and `__get()`) for
 retrieving their properties, it will always return an empty array.

 Here's a quick example:

 {{{

 add_action( 'init', function() {

 class Thing {
     public $data = array();

     public function __isset( $key ) {
         return isset( $this->data[ $key ] );
     }
     public function __get( $key ) {
         return isset( $this->data[ $key ] ) ? $this->data[ $key ] : null;
     }
     public function __set( $key, $value ) {
         $this->data[ $key ] = $value;
     }
     public function __unset( $key ) {
         unset( $this->data[ $key ] );
     }
 }

 $thing1 = new Thing;
 $thing1->greeting = 'hello';

 $thing2 = new Thing;
 $thing2->greeting = 'howdy';

 $test = array( $thing1, $thing2 );

 wp_die(
     var_dump(
         wp_filter_object_list( $test, array( 'greeting' => 'howdy' ) )
     )
 );

 } );
 }}}

 I expected for this utility to be flexible enough to handle these cases,
 because a few WordPress classes (like `WP_User`) already use magic methods
 similar to my above example.

 The reason I expected that, is because it's already flexible enough to
 work with either an object or an array, even though most function names
 and documentation lean towards "object" an array will work just fine. In
 addition, there is no documentation stating that magic methods are not
 supported, leading me to believe they should be if they can be.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/50095>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list