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
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
36 + 25 = ?
Subscribe to this entry?

 
 [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: Thu Apr 25 20:01:45 2024 UTC