php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #78801 Add "arrayable" pseudo type hint to PHP
Submitted: 2019-11-09 14:48 UTC Modified: 2019-11-11 12:30 UTC
From: norbert at linuxnetworks dot de Assigned:
Status: Suspended Package: *General Issues
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: norbert at linuxnetworks dot de
New email:
PHP Version: OS:

 

 [2019-11-09 14:48 UTC] norbert at linuxnetworks dot de
Description:
------------
Since PHP 7.1 there's the "iterable" pseudo type hint that matches "array" and "Traversable".

It would be great if PHP would support an "arrayable" pseudo type hint that matches "array" and all objects that implements "Traversable", "ArrayAccess" and "Countable".

Thus, we could pass arrays or all objects that behave like arrays to methods and do:

function useArrayable( arrayable $arg ) : arrayable {
    count( $arg );
    $value = $arg['key'];
    foreach( $arg as $key => $entry ) { ... }
    return $arg;
}

It would be useful to implement an Arrayable interface too:

interface Arrayable extends \Iterator, \Countable, \ArrayAccess
{
    public function toArray() : array;
}

Then, we can use array like objects:

class Test implements \Arrayable
{
    // ...
}

$arrayable = new Test();
$arrayable['key'] = $value;
$arrayable = useArrayable( $arrayable );

And if necessary, we can convert them to arrays:

if( $arrayable instanceof \Arrayable ) {
    $arrayable = $arrayable->toArray();
}


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-11-11 12:30 UTC] cmb@php.net
-Status: Open +Status: Suspended
 [2019-11-11 12:30 UTC] cmb@php.net
This feature should be discussed on the internals mailing list[1]
(and might require an RFC).  Please forward your request to there.
For the time being, I'm suspending this ticket.

[1] <https://www.php.net/mailing-lists.php#internals>
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 14:01:34 2025 UTC