private function entityToReference(&$item, $key) {
    if ($this->isASleepingReference) {
      $this->referenceWake();
    }
    if ((is_a($item, '\Nymph\Entity') || is_a($item, '\SciActive\HookOverride'))
        && isset($item->guid) && is_callable([$item, 'toReference'])) {
      // This is an entity, so we should put it in the entity cache.
      if (!isset($this->entityCache["referenceGuid__{$item->guid}"])) {
        $this->entityCache["referenceGuid__{$item->guid}"] = clone $item;
      }
      // Make a reference to the entity (its GUID and the class the entity was
      // loaded as).
      $item = $item->toReference();
    }
  }