Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. DeepCopy.php

function DeepCopy::copyObject

Copies an object.

Parameters

object $object:

Return value

object

Throws

CloneException

1 call to DeepCopy::copyObject()
DeepCopy::recursiveCopy in vendor/myclabs/deep-copy/src/DeepCopy/DeepCopy.php

File

vendor/myclabs/deep-copy/src/DeepCopy/DeepCopy.php, line 179

Class

DeepCopy
@final

Namespace

DeepCopy

Code

private function copyObject($object) {
    $objectHash = spl_object_hash($object);
    if (isset($this->hashMap[$objectHash])) {
        return $this->hashMap[$objectHash];
    }
    $reflectedObject = new ReflectionObject($object);
    $isCloneable = $reflectedObject->isCloneable();
    if (false === $isCloneable) {
        if ($this->skipUncloneable) {
            $this->hashMap[$objectHash] = $object;
            return $object;
        }
        throw new CloneException(sprintf('The class "%s" is not cloneable.', $reflectedObject->getName()));
    }
    $newObject = clone $object;
    $this->hashMap[$objectHash] = $newObject;
    if ($this->useCloneMethod && $reflectedObject->hasMethod('__clone')) {
        return $newObject;
    }
    if ($newObject instanceof DateTimeInterface || $newObject instanceof DateTimeZone) {
        return $newObject;
    }
    foreach (ReflectionHelper::getProperties($reflectedObject) as $property) {
        $this->copyObjectProperty($newObject, $property);
    }
    return $newObject;
}
RSS feed
Powered by Drupal