eboreum / cloner
Immutably clone (copy) objects and override their properties. Works for readonly classes and properties as well.
Requires
- php: ^8.5
- eboreum/caster: ^3.0
- eboreum/immutable-interface: ^1.0
Requires (Dev)
- overtrue/phplint: ^9.7
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^13.1
- slevomat/coding-standard: ^8.27
- squizlabs/php_codesniffer: ^4.0
This package is auto-updated.
Last update: 2026-05-17 14:29:27 UTC
README

Immutably clone (copy) objects and override their properties. Works for readonly classes and properties as well.
Optionally, add custom logic in a __clone method in your own class to apply regular cloning logic. The logic in the __clone method is executed after values have been copied to a clone.
Requirements
"php": "^8.5",
"eboreum/caster": "^3.0",
"eboreum/immutable-interface": "^1.0"
Installation
Via Composer (https://packagist.org/packages/eboreum/cloner):
composer require eboreum/cloner
Via GitLab:
git clone git@gitlab.com:eboreum/cloner.git
Alternatives
myclabs/deep-copy: The nuclear option.spatie/php-cloneable: Has uncovered edge cases. See reasoning further down.
License & Disclaimer
See LICENSE file. Basically: Use this library at your own risk.
Contributing
We prefer that you create an issue and or a merge request at https://gitlab.com/eboreum/cloner, and have a discussion about a feature or bug here.
Credits
Authors
- Kasper Søfren (kafoso)
E-mail: soefritz@gmail.com
Homepage: https://gitlab.com/kafoso / https://github.com/kafoso
Acknowledgements
This implementation is inspired by https://packagist.org/packages/spatie/php-cloneable. However, there are five main differences to the Spatie implementation.
These five differences are:
- The "
continue" on line 26 seems like a bug in that it silently ignores unmatched keys in the$valuesargument. - The "
bindTo" call on 34 may cause aTypeError. This results in sequential error discovery (one at a time). - In
eboreum/cloner(this codebase), we walk parent classes to clone parent-scoped properties as well. Something thatspatie/php-cloneabledoes not do, which can lead to undesired behavior. - In
spatie/php-cloneable, dynamic properties are not transferred. - In
eboreum/cloner(this codebase),__cloneis called when an object defines it.
In the implementation in eboreum/cloner (this codebase):
- Instead of a single
TypeError(once per faulty property value), we aggregate all type problems and report all at once. - If a child class and a parent class both declare a property with the same name, only the first-discovered property may be overridden from $values; later parent-scoped properties with that name are copied from the original object.
- Dynamic properties are transferred, which mirrors the behavior of
clone $myObject. - The
__clonemethod is invoked using a closure bound to the method's declaring class.