php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #80799 Support ArrayAccess in array_column()
Submitted: 2021-02-25 12:17 UTC Modified: 2021-03-01 14:15 UTC
Votes:2
Avg. Score:3.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:0 (0.0%)
From: saribekyantaron at gmail dot com Assigned:
Status: Open Package: Class/Object related
PHP Version: Next Minor Version OS: Any
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: saribekyantaron at gmail dot com
New email:
PHP Version: OS:

 

 [2021-02-25 12:17 UTC] saribekyantaron at gmail dot com
Description:
------------
---
From manual page: https://php.net/function.array-column
---
The first parameter describes: 
A multi-dimensional array or an array of objects from which to pull a column of values from. If an array of objects is provided, then public properties can be directly pulled. In order for protected or private properties to be pulled, the class must implement both the __get() and __isset() magic methods.
But I think if object's class implements interface \ArrayAccess, no need to define magic methods (set, get, isset) for allowing them to be used in array-management functions: like array_columns. I mean that Array*** interfaces can be enough (I will prefer required) to use objects or object arrays in array_*** functions.

Test script:
---------------
class SimpleArrayLikeClass implements \ArrayAccess
{
    public function offsetExists($name)
    {
        return array_key_exists($name, $this->attributes());
    }

    public function offsetGet($name)
    {
        return $this->attributes()[$name] ?? null;
    }

    // if object need to be used as array this 2 methods need to be used instead of __isset() and __get() 
}

Expected result:
----------------
In future versions became deprecated to use objects as array, if they not implemented Array*** interfaces, instead of magic methods


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-03-01 14:15 UTC] nikic@php.net
-Summary: Wrong behavior for function array_column +Summary: Support ArrayAccess in array_column()
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 27 00:01:30 2024 UTC