-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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
Clarify Collections' interaction with Traversible<object, mixed>, such as WeakMap #49089
Comments
…akMap` fixes #49089 Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
Submitted a PR to throw exception when trying to use |
Slow down! The problem is not that trivial. I did some more research after submitting this, and realized Basically, any class that is We will never know how many such incompatible classes out there that extends Therefore, the question remains:
|
I noticed this flag in iterator_to_array:
Will setting it to false help with the situation? I see mentions of doing so to give out items inside |
You can't convert WeakMap to an array and we not planning to add support for WeakMap using Collection at the moment. |
…akMap` (#49095) * [10.x] Throw exception when trying to initiate `Collection` using `WeakMap` fixes #49089 Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> * Apply fixes from StyleCI * wip Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> * Update SupportCollectionTest.php * Update EnumeratesValues.php --------- Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> Co-authored-by: StyleCI Bot <bot@styleci.io> Co-authored-by: Taylor Otwell <taylor@laravel.com>
…akMap` (#49095) * [10.x] Throw exception when trying to initiate `Collection` using `WeakMap` fixes laravel/framework#49089 Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> * Apply fixes from StyleCI * wip Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> * Update SupportCollectionTest.php * Update EnumeratesValues.php --------- Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> Co-authored-by: StyleCI Bot <bot@styleci.io> Co-authored-by: Taylor Otwell <taylor@laravel.com>
Laravel Version
10.33
PHP Version
8.1.5
Database Driver & Version
N/A
Description
Laravel has
collect()
to produce a Collection; currently, the Collection accepts many types, one of which is PHP'sTraversible
.It looks fine, but a problem:
WeakMap
implementsTraversible
. From the constructor definition/hinting,WeakMap
instances are valid Collection "items". But as soon as such Collections are instantiated, a fatal error is always thrown:Clearly, the underlying data structure of the Collection is an array, which obviously cannot accept objects as keys.
This is quite undocumented in the official Laravel docs in the Laravel website. However, the existing Collection constructor did not really check against WeakMaps.
I do know a bit about the covariance-contravariance thing that makes it a bit difficult to specifically reject
WeakMap
instances during typehinting.A clarification is very much appreciated:
WeakMap
, then perhaps the docs should be updated with this info; or, a more specific error message could be printed.WeakMap
, then perhaps the Collection class needs some refactoring.One very jarring experience with Laravel, is that e.g. I can do
filter()
on "real array" Collections, but I cannot dofilter()
onWeakMap
instances, even when in native PHP, both can be traversed with something likeSteps To Reproduce
Using
php artisan tinker [file]
:File contents:
The text was updated successfully, but these errors were encountered: