- How do I install hyperf/collection with Composer?
- Run composer require hyperf/collection and the package autoloads under the Hyperf\Collection namespace. There is no build step, no service provider to publish, and no configuration file, because it is a pure userland library. In a Hyperf application the collection type is often already available through the framework, so check your composer.json before adding it explicitly.
- Is hyperf/collection the same as Laravel's Illuminate collection?
- It is a namespace-shifted fork of illuminate/collections, so the fluent API (map, filter, reduce, groupBy, sortBy, pluck, chunk, flatten) behaves like the Laravel snapshot it was forked from. The practical difference is the namespace: Hyperf\Collection instead of Illuminate\Support\Collection. Parity reflects the fork point rather than a live guarantee, since upstream Laravel keeps adding collection methods.
- Which PHP and Laravel versions does hyperf/collection support?
- The package targets PHP 8.1 and later and is plain userland code with a small dependency surface. Its versions ship as part of the Hyperf release train, so constraints track Hyperf rather than a specific Laravel release. Pin an explicit version in composer.json and read the changelog before upgrading.
- Can I use hyperf/collection in a plain PHP or Laravel application?
- Technically yes, because the Collection class works without Swoole or the Hyperf container. Outside Hyperf, though, it offers no functional advantage over illuminate/collections, and you would be introducing a second collection type under a different namespace. For standalone PHP or an existing Laravel monolith, upstream illuminate/collections is the better-evidenced choice.
- Does hyperf/collection require Swoole or coroutines?
- No. The Collection class is pure PHP with no Swoole or Swow requirement, so ordinary synchronous code can use it. Coroutine-specific behaviour comes from surrounding Hyperf services rather than from this package. Framework coupling does rise once you adopt Hyperf release-train version constraints across a service.
- How do I avoid conflicts between Hyperf\Collection and Illuminate\Support\Collection?
- Choose one canonical collection type per repository and enforce it, because type hints, instanceof checks and helper functions like collect() resolve to whatever class you imported. Two coexisting implementations are the main integration friction, particularly for shared DTOs relying on Arrayable and Jsonable contracts, which are implemented but not interchangeable across type declarations. A namespace convention plus a static-analysis rule in PHPStan or Psalm is usually enough to contain the problem.
- Does hyperf/collection support lazy collections for large datasets?
- Yes, it offers lazy collections alongside the eager chainable methods, so you can defer work over large datasets or generators. That suits reports, exports and data pipelines where materialising everything in memory is wasteful. Results stay JSON-friendly for APIs because the underlying items are plain arrays and scalars.
- How do I add custom methods to hyperf/collection with macros?
- Register methods with Collection::macro('name', $callback). Macros are global, mutable, static state, so they apply to every collection instance in the process and should be registered once in a service provider or bootstrap file. In long-running Hyperf workers, repeated registration is harmless but untidy, so centralise or guard it.
- How do I test code that uses hyperf/collection?
- You rarely need to mock the collection: build a real instance from a fixture array and assert on the returned values, since transformations return new instances and equality checks are straightforward. Where code type-hints Hyperf\Collection\Collection, construct the real object in tests rather than a stub. This also avoids the trap of contracts being implemented but not interchangeable across collection types.
- Is hyperf/collection actively maintained, and what are the alternatives?
- Maintenance rests with the Hyperf core team, whose attention is spread across dozens of split packages, and changelog entries are largely bare compare links, which limits triage of patch upgrades. The large star count belongs to the hyperf/hyperf monorepo rather than this package, so check Packagist download stats for real adoption signals. If you are not on Hyperf, illuminate/collections is the safer pick; if you are, the exit cost is a namespace swap across call sites, not a rewrite.