OmitEmpty
extends ObjAttribute
in package
Attribute to omit uninitialized properties from serialization.
When a property is marked with #[OmitEmpty], it will not be included in the serialized output if the property has not been assigned a value (checked via ReflectionProperty::isInitialized()).
Useful for optional fields that should not appear in JSON when not set.
Usage:
class User extends Obj {
public string $name;
#[OmitEmpty]
public ?string $phone = null;
}
$user = new User(['name' => 'Alice']);
// Serializes to: {"name": "Alice"} (phone is omitted)
Tags
Attributes
- #[Attribute]
- \Attribute::TARGET_PROPERTY
Table of Contents
Methods
- of() : static|false
- Extract an attribute instance from a reflected property.
Methods
of()
Extract an attribute instance from a reflected property.
public
static of(ReflectionProperty $field) : static|false
Searches the property's attributes for one matching this class name and instantiates it with the attribute's arguments.
Parameters
- $field : ReflectionProperty
-
The property to inspect for attributes.
Return values
static|false —The attribute instance if found, false otherwise.