Obj
in package
implements
JsonSerializable
Base class for serializable/deserializable API data objects.
Obj is the foundation of phpEZ's data handling. It provides automatic bidirectional serialization using PHP reflection and type hints, eliminating boilerplate mappers, validators, and serializers.
Key features:
- Automatic serialization: Reflects on properties to build JSON output
- Type-aware deserialization: Enforces types and converts nested objects
- Attribute-based control: Use #[OmitEmpty], #[DoNotSerialize], etc.
- Nested object support: Automatically handles Obj subclasses
- Custom type handling: Supports Parsable interface for specialized types
- Validation: Enforces nullability and type requirements
Usage:
class CreateUserRequest extends Obj {
public string $name;
public string $email;
#[OmitEmpty]
public ?string $phone = null;
}
// Deserialization (from API input):
$req = new CreateUserRequest(json_decode($body, true));
// Serialization (to API response):
$response = new UserResponse(...);
echo json_encode($response); // Calls __serialize() via JsonSerializable
Type system:
- Properties MUST have type declarations (string, int, bool, float, null, or Obj subclass)
- Type hints are enforced during deserialization
- Nullable types (?Type) are allowed
- Use #[OmitEmpty] for optional fields
Tags
Table of Contents
Interfaces
- JsonSerializable
Constants
- baseTypes : array<string|int, string> = ['string', 'int', 'bool', 'float', 'null']
- Base types that don't require special serialization handling.
Methods
- __construct() : mixed
- Initialize an Obj, optionally populating it with data.
- __serialize() : array<string, mixed>
- Serialize this object to an array suitable for JSON encoding.
- __unserialize() : void
- Deserialize data into this object's properties.
- jsonSerialize() : mixed
- Implement JsonSerializable to enable json_encode() on this object.
- serializeVal() : mixed
- unserializeRaw() : void
- Deserialize a JSON string into this object.
Constants
baseTypes
Base types that don't require special serialization handling.
protected
array<string|int, string>
baseTypes
= ['string', 'int', 'bool', 'float', 'null']
Methods
__construct()
Initialize an Obj, optionally populating it with data.
public
__construct([mixed $data = null ]) : mixed
If $data is provided, it's passed to __unserialize() to populate properties.
This enables single-line initialization: new User($data)
Parameters
- $data : mixed = null
-
Optional data to deserialize into this object. Usually an array from json_decode() or similar.
__serialize()
Serialize this object to an array suitable for JSON encoding.
public
__serialize() : array<string, mixed>
Reflects on all properties and processes each according to:
- Skips static properties and those with #[DoNotSerialize]
- Skips uninitialized properties with #[OmitEmpty]
- For Parsable fields: calls marshall()
- For objects with __serialize(): calls __serialize()
- For base types (string, int, bool, float, null): includes as-is
Returns a flat array with property names as keys.
Tags
Return values
array<string, mixed> —Serialized representation suitable for json_encode().
__unserialize()
Deserialize data into this object's properties.
public
__unserialize(mixed $data) : void
Reflects on all properties and populates them from the input data array:
- Skips static properties and those with #[DoNotDeserialize]
- Validates mandatory fields (non-nullable types with no value)
- For #[OmitEmpty] fields: allows missing data
- For Parsable types: calls parse()
- For objects with __unserialize(): recursively deserializes
- For base types: assigns directly (with type coercion)
Enforces type safety: throws HTTPException (400) for validation failures, HTTPException (500) for unsupported types.
Parameters
- $data : mixed
-
Array-like data to deserialize (typically from json_decode(array)).
Tags
Return values
void —Properties are populated in-place.
jsonSerialize()
Implement JsonSerializable to enable json_encode() on this object.
public
jsonSerialize() : mixed
Delegates to __serialize() so that json_encode($obj) works seamlessly.
Return values
mixed —The serialized array from __serialize().
serializeVal()
public
static serializeVal(mixed $val[, string|null $type = null ]) : mixed
Parameters
- $val : mixed
- $type : string|null = null
unserializeRaw()
Deserialize a JSON string into this object.
public
unserializeRaw(string $rawData) : void
Convenience method that decodes raw JSON and calls __unserialize(). Used by the API framework to populate request body objects.
Parameters
- $rawData : string
-
Raw JSON string from request body.
Tags
Return values
void —Properties are populated in-place.