[php-src] Issue #16038: Make objects unpackable by default
| From: | MrMeshok | Date: | Wed, 25 Sep 2024 04:45:04 +0000 |
| Subject: | [php-src] Issue #16038: Make objects unpackable by default | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-247891@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/16038
Author: MrMeshok
### 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
```php
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