eboreum/cloner

Immutably clone (copy) objects and override their properties. Works for readonly classes and properties as well.

Maintainers

Package info

gitlab.com/eboreum/cloner

Issues

pkg:composer/eboreum/cloner

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 0

dev-main 2026-05-17 16:22 UTC

This package is auto-updated.

Last update: 2026-05-17 14:29:27 UTC


README

license pipeline status coverage report PHPStan Level

eboreum-cloner

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

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

Acknowledgements

This implementation is inspired by https://packagist.org/packages/spatie/php-cloneable. However, there are five main differences to the Spatie implementation.

See: https://github.com/spatie/php-cloneable/blob/8760f6ce285a1016075c583eae764f562e214b6d/src/Cloneable.php

These five differences are:

  1. The "continue" on line 26 seems like a bug in that it silently ignores unmatched keys in the $values argument.
  2. The "bindTo" call on 34 may cause a TypeError. This results in sequential error discovery (one at a time).
  3. In eboreum/cloner (this codebase), we walk parent classes to clone parent-scoped properties as well. Something that spatie/php-cloneable does not do, which can lead to undesired behavior.
  4. In spatie/php-cloneable, dynamic properties are not transferred.
  5. In eboreum/cloner (this codebase), __clone is 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 __clone method is invoked using a closure bound to the method's declaring class.