Download the PHP package dcarbone/php-object-merge without Composer
On this page you can find all versions of the php package dcarbone/php-object-merge. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download dcarbone/php-object-merge
More information about dcarbone/php-object-merge
Files in dcarbone/php-object-merge
Package php-object-merge
Short Description Object property merging in PHP
License Apache-2.0
Homepage https://github.com/dcarbone/php-object-merge
Informations about the package php-object-merge
php-object-merge
This is a simple library that facilitates the merging of two or more PHP stdClass
object properties
Non-Recursive
The fastest approach to merging two objects will simply apply the fields present in the list of ...$others
to the root object.
Recursive
If you require recursive merging of child objects, that is also possible:
Callback
If you wish to sometimes manually handle the merging of two values, you may do so using the provided _callback
functions.
Callback providing
You may provide any php-callable notation you wish, including:
Callback arguments
The callback function will be provided exactly one parameter, and it will always be an instance of ObjectMergeState.
Callback response
If the callback function returns anything other than an instance of ObjectMergeResult, it is used outright as the value of the merge, without further processing or recursion.
See comments on ObjectMergeResult for how each parameter is handled.
Merge Options
The object_merge
and object_merge_recursive
functions have sister functions named object_merge_opts
and
object_merge_recursive_opts
respectively. Each of these has a required $opts
argument that must be a bitwise
inclusive or of your desired options.
OBJECT_MERGE_OPT_CONFLICT_OVERWRITE
This is the default option.
This means that when the provided root object already has a field seen in one of the ...$others
, the value of the LAST
of the $others
objects will ultimately be used
Example:
OBJECT_MERGE_OPT_CONFLICT_EXCEPTION
When this is provided, an exception will be raised if there is a type mismatch
Example:
OBJECT_MERGE_OPT_UNIQUE_ARRAYS
NOTE: This only has an effect when doing a recursive merge!
When this is provided, any seen array value that does not have a type conflict with an existing field type has its value
pass through array_values(array_unique($v))
.
This has the net effect of returning a re-indexed array consisting of only unique values.
Example:
OBJECT_MERGE_OPT_COMPARE_ARRAYS
NOTE: This only has an effect during a recursive merge!
When this is provided, individual array offsets will have their values compared and merged, rather than merely appended together.
Example 1:
Example 2:
OBJECT_MERGE_OPT_NULL_AS_UNDEFINED
When specified, NULL values are treated as UNDEFINED, meaning they will not cause a type conflict to be risen and the non-null value will be used in the merge.
This can be useful if you do not want to have all type conflicts to be overwritten with the right-hand value, but want to ignore null to not null differences.
Example: