DataObject documentation
DataObject is a small immutable container for structured data.
It is designed for the common case where we want to move data around an application without exposing a mutable array or stdClass to every part of the codebase.
The public API is centred around two classes:
GT\DataObject\DataObject, which stores and reads the dataGT\DataObject\DataObjectBuilder, which createsDataObjectinstances from existing PHP structures
What this library does
- Stores key/value data in an immutable object.
- Provides typed getters such as
getString(),getInt()andgetDateTime(). - Supports nested
DataObjectinstances. - Converts back to arrays, plain objects and JSON.
- Builds data objects from associative arrays or standard objects.
- Checks array contents against primitive or class types when required.
Where this library fits in
This package is fully standalone:
composer require phpgt/dataobject
It is also installed as a dependency of PHP.GT/WebEngine, so you can use it directly in WebEngine applications without extra setup. The examples in this guide stay framework-independent so the API is clear in isolation.
Other PHP.GT packages also build on the same idea. For example, PHP.GT/Fetch returns structured response objects, and PHP.GT/Json extends this package for JSON-specific behaviour.
A tiny example
use GT\DataObject\DataObject;
$user = (new DataObject())
->with("id", 105)
->with("name", "Cody")
->with("active", true);
echo $user->getInt("id"), PHP_EOL;
echo $user->getString("name"), PHP_EOL;
echo $user->getBool("active") ? "yes" : "no", PHP_EOL;
Each call to with() returns a new object, leaving the previous instance unchanged.
Continue with Quick start to install the package and build the first DataObject.