Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make objects unpackable by default #16038

Closed
MrMeshok opened this issue Sep 25, 2024 · 3 comments
Closed

Make objects unpackable by default #16038

MrMeshok opened this issue Sep 25, 2024 · 3 comments

Comments

@MrMeshok
Copy link

Description

Right now if you try to unpack an object, you will get an error: Only arrays and Traversables can be unpacked.
But if you just foreach an object, it works, and you can iterate over each public property, so I think unpacking should work on all objects and return public properties. This would be especially nice when working with stdClass or DTO.
And take a look at an example  where objects share some properties

readonly class Balance
{
    public function __construct(
        public float $debt,
        public float $credit,
    ) {}
}

readonly class Profile
{
    public function __construct(
        public string $name,
        public float $debt,
        public float $credit,
    ) {}
}

$balance = new Balance(
    debt: 2,
    credit: 1,
);

$profile = new Profile(
    ...$balance,
    name: 'Name',
);

Right now workaround is to use ...(array) $balance or implement IteratorAggregate on classes

@iluuu1994
Copy link
Member

Hi @MrMeshok! Thank you for the suggestion. Language changes require RFCs, and while we do allow feature requests on the bug tracker, they often don't go anywhere. The proactive thing would be to start a discussion on the internals mailing list, to see if there's interest in this change. Is that something you're interested in doing?

https://www.php.net/mailing-lists.php

@MrMeshok
Copy link
Author

Hi @iluuu1994, I will try to look into that. Did I get it right, that I should write an email to [email protected]?

@iluuu1994
Copy link
Member

Yes, precisely. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants