Skip to content
This repository was archived by the owner on Jan 2, 2023. It is now read-only.

Static constructor for Collection/Resource? #54

Closed
tobyzerner opened this issue Oct 6, 2015 · 4 comments
Closed

Static constructor for Collection/Resource? #54

tobyzerner opened this issue Oct 6, 2015 · 4 comments

Comments

@tobyzerner
Copy link
Owner

Chaining a "new Collection" call is a bit awkward because it has to be surrounded in parentheses. I wonder if this syntax ...

$collection = (new Collection($posts, new PostSerializer))
    ->with(['author', 'comments']);

... could be improved with something like:

$collection = Collection::make($posts, new PostSerializer)
    ->with(['author', 'comments']);

make is pretty standard, another idea is Collection::of() (but Resource::of() doesn't work quite as well). Any other ideas/thoughts?

@vinkla
Copy link
Contributor

vinkla commented Oct 7, 2015

Maybe we can provide both?

@byCedric
Copy link

Technically, there is nothing to worry about. This "construct-and-chain" is available since 5.4+, the package itself is supporting 5.5.9+. So that makes this only a "sugar coating" of the syntax.
There are a lot of people who say static methods undermines the OOP principle, by exporting a lose method. Or static methods are hard to test.

Personally, I think both will do just fine. Just like Symfony and Laravel, you can create a Request object using Request::create or new Request. Regarding the namespace, I think make or create is the best option since it's defining literally what it's supposed to do. Although of sounds better in a sentence, some could think it has something to do with detecting collection (instance) of ....

TL;DR; +1 for both, using make, create or another explicit definition.

@gossi
Copy link

gossi commented Apr 18, 2016

I also provide Obj::create(...) on my classes which just looks better for method chaining. I also found this kind of code in other projects, too. So far in the packages I used, it's always create, which I'd decided to go with. of is more some sort of it's created by some similar type of (e.g. Set::of($collection)).

@tobyzerner
Copy link
Owner Author

This will be made a non-issue with the new API coming as part of #115.

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

No branches or pull requests

4 participants