-
-
Notifications
You must be signed in to change notification settings - Fork 2
Examples
Greg Bowler edited this page Jun 20, 2023
·
3 revisions
use Gt\DataObject\DataObject;
$obj = (new DataObject())
->with("name", "Cody")
->with("colour", "orange")
->with("food", [
"biscuits",
"mushrooms",
"corn on the cob",
]);
echo json_encode($obj), PHP_EOL;Output:
{"name":"Cody","colour":"orange","food":["biscuits","mushrooms","corn on the cob"]}use Gt\DataObject\DataObjectBuilder;
$jsonString = '{"name":"Cody","colour":"orange","food":["biscuits","mushrooms","corn on the cob"]}';
$builder = new DataObjectBuilder();
$obj = $builder->fromObject(json_decode($jsonString));
echo "Hello, ",
$obj->getString("name"),
"! Your favourite food is ",
$obj->getArray("food")[0],
PHP_EOL;Output:
Hello, Cody! Your favourite food is biscuits
PHP.Gt/DataObject is a separately maintained component of PHP.Gt/WebEngine.