RuntimePublicReflectionProperty.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace Doctrine\Common\Reflection;
  3. use Doctrine\Common\Proxy\Proxy;
  4. use ReflectionProperty;
  5. /**
  6. * PHP Runtime Reflection Public Property - special overrides for public properties.
  7. *
  8. */
  9. class RuntimePublicReflectionProperty extends ReflectionProperty
  10. {
  11. /**
  12. * {@inheritDoc}
  13. *
  14. * Checks is the value actually exist before fetching it.
  15. * This is to avoid calling `__get` on the provided $object if it
  16. * is a {@see \Doctrine\Common\Proxy\Proxy}.
  17. */
  18. public function getValue($object = null)
  19. {
  20. $name = $this->getName();
  21. if ($object instanceof Proxy && ! $object->__isInitialized()) {
  22. $originalInitializer = $object->__getInitializer();
  23. $object->__setInitializer(null);
  24. $val = $object->$name ?? null;
  25. $object->__setInitializer($originalInitializer);
  26. return $val;
  27. }
  28. return isset($object->$name) ? parent::getValue($object) : null;
  29. }
  30. /**
  31. * {@inheritDoc}
  32. *
  33. * Avoids triggering lazy loading via `__set` if the provided object
  34. * is a {@see \Doctrine\Common\Proxy\Proxy}.
  35. * @link https://bugs.php.net/bug.php?id=63463
  36. */
  37. public function setValue($object, $value = null)
  38. {
  39. if (! ($object instanceof Proxy && ! $object->__isInitialized())) {
  40. parent::setValue($object, $value);
  41. return;
  42. }
  43. $originalInitializer = $object->__getInitializer();
  44. $object->__setInitializer(null);
  45. parent::setValue($object, $value);
  46. $object->__setInitializer($originalInitializer);
  47. }
  48. }