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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 01:01:28 2024 UTC