phpEZ v0.4.0

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
see
JsonSerializable
OmitEmpty
DoNotSerialize
DoNotDeserialize
Parsable
subpackage

iface

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:

  1. Skips static properties and those with #[DoNotSerialize]
  2. Skips uninitialized properties with #[OmitEmpty]
  3. For Parsable fields: calls marshall()
  4. For objects with __serialize(): calls __serialize()
  5. For base types (string, int, bool, float, null): includes as-is

Returns a flat array with property names as keys.

Tags
throws
HTTPException

For unsupported field types or serialization errors.

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:

  1. Skips static properties and those with #[DoNotDeserialize]
  2. Validates mandatory fields (non-nullable types with no value)
  3. For #[OmitEmpty] fields: allows missing data
  4. For Parsable types: calls parse()
  5. For objects with __unserialize(): recursively deserializes
  6. 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
throws
HTTPException

For validation errors (400) or unsupported types (500).

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
throws
HTTPException

(400) For invalid JSON or deserialization errors.

example
$request = new CreateUserRequest();
$request->unserializeRaw(file_get_contents('php://input'));
Return values
void

Properties are populated in-place.

On this page

Search results