You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently Collection and EnumeratesValues contain multiple instances (Collections - 73 and EnumeratesValues - 7) of new static to instantiation a new instance of the class to return a "fresh" object.
This causes a problem if the class is extended with a constructor with more than 1 parameter. Currently, to extend their you would need to rewrite every method that directly instantiates a new instance rather than simply extending Collection and making your unique changes.
For example the following situation is not supported:
class CsvCollection extends Collection
{
public function __construct(public readonly string $name, $items = [])
{
parent::__construct($items);
}
}
$csv = new CsvCollection('test');
$file = new SplFileObject("data.csv");
while (!$file->eof()) {
$csv->push($file->fgetcsv());
}
$cleanCsv = $csv->filter();
To support the above situation every instance of new static (except EnumeratesValues::make()) would need to be replaced with static::make() for static methods and the new function EnumeratesValues::newInstance($items = []) for instantiated instances. This would apply to Eloquent/Collection as well. The naming convention would align with Model::newInstance() for conceptual alignment.
Implications:
Trace stack would be a bit more complex for exceptions thrown in callbacks provided to Collections
There may be issues with static analysis (I haven't tested so this may not be true)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Currently Collection and EnumeratesValues contain multiple instances (Collections - 73 and EnumeratesValues - 7) of
new static
to instantiation a new instance of the class to return a "fresh" object.This causes a problem if the class is extended with a constructor with more than 1 parameter. Currently, to extend their you would need to rewrite every method that directly instantiates a new instance rather than simply extending Collection and making your unique changes.
For example the following situation is not supported:
To support the above situation every instance of
new static
(exceptEnumeratesValues::make()
) would need to be replaced withstatic::make()
for static methods and the new functionEnumeratesValues::newInstance($items = [])
for instantiated instances. This would apply to Eloquent/Collection as well. The naming convention would align withModel::newInstance()
for conceptual alignment.Implications:
I'll make the PR if there's buy-in.
Beta Was this translation helpful? Give feedback.
All reactions