shlogg · Early preview
Paul Redmond @paulredmond

Create Immutable Value Objects In Laravel With Bag Package

Bag is a PHP & Laravel package for Immutable Value Objects, providing type-safe data encapsulation with built-in validation & casting. Replace arrays with Bag objects to benefit from strong typing.

Bag is a PHP and Laravel package for Immutable Value Objects, inspired by Spatie's laravel-data package. It can help you create immutable objects to encapsulate your data in a type-safe way with data casts and built-in validation. The package suggests it can be used to replace regular arrays in your code to benefit from type safety.
use Bag\Bag; readonly class MyValue extends Bag {    public function __construct(public string $name, public int $age) {}} $value = MyValue::from([    'name' => 'Davey Shafik',    'age' => 40,]); // Create a new instance with a different value$newValue = $value->wi...