[wp-trac] [WordPress Trac] #55105: Introduce a polyfill for `array_is_list()`.

WordPress Trac noreply at wordpress.org
Thu Oct 27 22:04:53 UTC 2022


#55105: Introduce a polyfill for `array_is_list()`.
--------------------------------------+-------------------------
 Reporter:  costdev                   |       Owner:  (none)
     Type:  feature request           |      Status:  closed
 Priority:  normal                    |   Milestone:
Component:  General                   |     Version:
 Severity:  minor                     |  Resolution:  maybelater
 Keywords:  has-patch has-unit-tests  |     Focuses:
--------------------------------------+-------------------------

Comment (by johnjamesjacoby):

 Some random related thoughts...

 ----

 See also: `wp_is_numeric_array()` & `rest_is_array()` for similar but
 different examples of functions in WordPress that are documented to imply
 their intention ''may have been'' to mimic the behavior of
 `array_is_list()` but perhaps a consult from @rmccue would be helpful?

 The above helper function(s) `@return bool Whether the variable is a list`
 but do not enforce @costdev's first point:

 > A **list ''must''** start from an offset of 0
 ''(emphasis, mine... but I think I agree?)''

 While a bunch of other programming languages explicitly define & strictly
 enforce "List" and "Array" as different things (C, Java, Python, etc...)
 neither PHP nor WordPress do currently.

 ''I think'' that means it is ''safe to suggest'' that WordPress functions
 named like `wp_parse_list()` and `wp_list_*()` – and the `WP_List_Util()`
 class – ''largely imply'' that they are ''intended'' to work with either
 arrays or objects. Similarly, only the REST API (and later the Meta API
 via default values in `get_metadata_default()`) have experienced a need
 for identifying whether a variable value was a numerically indexed array
 that starts with 0 and does not omit any subsequent integers as keys until
 the final key.

 ----

 Without speculating too deeply, PHP core is trending towards becoming more
 strict (hence more static), so perhaps someday a "List" could be a more
 explicitly defined thing, and it may behoove WordPress core contributors
 to have a plan for that and decide whether to:

 * Keep down the path of generalized "lists" as arrays or objects
 * Add to the global namespace `list` functions & classes that mean what
 `array_is_list()` means

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/55105#comment:13>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list