Beacon-hq/Bag: Immutable Value Objects for Modern PHP

If you've ever wished PHP had cleaner, safer, and more predictable ways to handle structured data, Bag might become your new favorite tool. Created by Davey Shafik, Bag gives you a super lightweight way to build immutable, strongly typed value objects — without the noise, the boilerplate, or the framework lock-in.
And yes… it plays beautifully with Laravel out of the box.
What Is Bag?
Bag is a tiny PHP 8.3+ package that lets you define immutable value objects with:
- Strong typing
- Built-in validation
- Input/output casting
- Collection support
- Easy composition (nest value objects inside each other)
In other words: Stop passing messy arrays around your app — and start working with clean, predictable objects instead.
Why Use Value Objects?
When your application grows, plain arrays become a silent source of bugs:
- Fields get renamed
- Data shape changes
- Types become inconsistent
- Validation logic gets duplicated everywhere
Value objects fix all of that by enforcing structure and type-safety at the edges of your system.
Bag takes that idea and makes it incredibly smooth to implement.
How It Works (Quick Example)
Define a Value Object
use Bag\Bag; readonly class MyValue extends Bag { public function __construct( public string $name, public int $age, ) {} }
Instantiate It
$data = MyValue::from([ 'name' => 'Davey Shafik', 'age' => 40, ]);
That’s it — clean, immutable, validated data, every time.
