Download the PHP package om/from-array without Composer
On this page you can find all versions of the php package om/from-array. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download om/from-array
More information about om/from-array
Files in om/from-array
Package from-array
Short Description fromArray trait allow create objects instances loaded with initial data array
License BSD-3-Clause
Informations about the package from-array
FromArray Data Loader Trait
FromArray is a lightweight PHP trait that enables effortless object hydration from associative arrays. It is
especially useful in scenarios where data needs to be mapped into strongly-typed objects, such as when working with
APIs, form inputs, or configuration data.
With support for PHP attributes, FromArray lets you fine-tune how each property is loaded, define type expectations,
and apply custom transformation logic — all while keeping your code clean and expressive.
Install
Usage
The FromArray trait enables the creation of object instances preloaded with an initial data array:
Result:
The trait works with public properties only — private and protected properties will be ignored.
Attributes
There are several PHP Attributes to
configure the fromArray behavior:
Property
The Property attribute allows you to map a specific key (from) from the data array to the property:
You can also customize the loader used to transform the data:
Loader can be the name of a class, an object, or any type of callable.
For more see basic-example.php.
Loader
The Loader attribute allows you to define a value loader for the entire class or for individual properties:
For more see loader-property.php.
Type
The Type attribute allows you to define a specific type for the property. Types are usually auto-detected, but you can
force the type using the Type attribute:
There is one special case — you can specify Types::ArrayOfObjects to load an array of objects of
the same class. The class parameter is required in this case:
See array-of-objects.php for more information.
Metadata
The Metadata object contains all the
information about the class and its properties. It is a singleton, but you can load an instance from cache:
See metadata-cache.php for more information.