php-standard-library/dict
Utility functions for working with PHP associative arrays (“dicts”): create, map, filter, and transform collections while preserving keys. Lightweight helpers from PHP Standard Library for cleaner, safer array manipulation.
Pros:
config/, app/) and dynamic data handling (e.g., Eloquent attributes, API payloads). Reduces reliance on raw arrays for associative data, improving code clarity and maintainability.dict->get('key') vs. $array['key']), reducing ambiguity and runtime errors (e.g., UndefinedIndex).Collection or Symfony’s OptionsResolver, making it ideal for simple key-value operations.foreach and collection-like iteration, easing adoption for teams familiar with Laravel’s ecosystem.Dict<string, mixed>), improving IDE support and static analysis.Cons:
Arrayable, Jsonable, or Macroable interfaces).dict->get()) may be slightly slower than raw array access in microbenchmarks, though negligible for most applications.map, reduce), or lazy loading, which may require external libraries.Dict as a singleton or per-request dependency.Dict instances.Arrayable methods or collection features), the package may require updates to stay aligned.Dict structures could complicate debugging (e.g., var dumps, logging), though this can be mitigated with custom serialization.Use Case Prioritization:
Dict provide the most value? (e.g., configuration management, dynamic attributes, request payloads, service classes).Dict?Migration Strategy:
Arrayable manually for API responses).Performance Impact:
Dict against raw arrays for critical paths (e.g., request processing, queue jobs)?Team Adoption:
Dict for new code, and how will we measure success? (e.g., lines of code using Dict, reduction in UndefinedIndex errors).Long-Term Maintenance:
JsonSerializable, Laravel-specific macros)?Laravel-Specific Use Cases:
config('app.*') with Dict instances (e.g., new Dict(config('app'))), enabling safer access with defaults and merging.Dict objects (e.g., Dict::fromArray($request->all())) to standardize input processing.Dict trait for dynamic attributes (e.g., user->metadata->dict()), reducing boilerplate for custom attributes.Dict to arrays/JSON via custom toArray()/toJson() methods or by implementing Laravel’s Arrayable/Jsonable interfaces.Dict for internal state management (e.g., new Dict($this->config)).Non-Laravel PHP:
Collection via adapters (e.g., Dict::fromCollection($collection)).Phase 1: Opt-In Adoption (Low Risk)
Dict for configuration bags (e.g., config('app') → new Dict(config('app'))).Dict::fromArray($data)).// Before
$theme = config('app.theme', 'light');
// After
$dict = new Dict(config('app'));
$theme = $dict->get('theme', 'light');
Dict methods.Phase 2: Enforce in Critical Paths (Medium Risk)
Dict instances for all dependencies.// BaseService.php
protected Dict $config;
public function __construct(array $config) {
$this->config = new Dict($config);
}
Phase 3: Full Migration (High Risk)
config() calls with Dict-wrapped config.Dict for dynamic attributes (e.g., via a trait).Arrayable/Jsonable for seamless API integration.// User.php
use App\Traits\HasDictAttributes;
class User extends Model {
use HasDictAttributes;
protected $attributesDict;
}
Laravel Integration:
Dict as a singleton or per-request dependency.
$this->app->singleton(Dict::class, function () {
return new Dict(config('app'));
});
Illuminate\Http\Request to return Dict instances.
Request::macro('toDict', function () {
return Dict::fromArray($this->all());
});
Dict support for dynamic attributes.Macroable or advanced collection features.PHP Ecosystem:
ArrayAccess and IteratorAggregate.JsonSerializable or Laravel’s Arrayable.| Priority | Component | Effort | Risk | Notes |
|---|---|---|---|---|
| 1 | Configuration | Low | Low | Replace config() with Dict wrappers. |
| 2 | Request Handling | Medium | Medium | Normalize input data into Dict. |
| 3 | Service Classes |
How can I help you explore Laravel packages today?