diff --git a/src/Threading/LocalObject.php b/src/Threading/LocalObject.php index 20a1f51..2f21280 100644 --- a/src/Threading/LocalObject.php +++ b/src/Threading/LocalObject.php @@ -114,6 +114,16 @@ class LocalObject implements \Serializable } } + /** + * Handles cloning, which creates clones the local object and creates a new + * local object handle. + */ + public function __clone() + { + $object = clone $this->deref(); + $this->__construct($object); + } + /** * Gets information about the object for debugging purposes. * diff --git a/tests/Threading/LocalObjectTest.php b/tests/Threading/LocalObjectTest.php index 1bde671..ec6dedc 100644 --- a/tests/Threading/LocalObjectTest.php +++ b/tests/Threading/LocalObjectTest.php @@ -91,4 +91,14 @@ class LocalObjectTest extends TestCase $thread->start(PTHREADS_INHERIT_INI); $thread->join(); } + + public function testCloneIsNewObject() + { + $object = new \stdClass(); + $local = new LocalObject($object); + $clone = clone $local; + $this->assertNotSame($local, $clone); + $this->assertNotSame($object, $clone->deref()); + $this->assertNotEquals($local->__debugInfo()['id'], $clone->__debugInfo()['id']); + } }