3v4l.org

run code in 300+ PHP versions simultaneously
<?php class ReminderPushEvents { /** * @var array */ private $properties; /** * @var string */ private $event = 'reminder_push_event'; /** * @var \DateTimeInterface */ private $timestamp; /** * Constructor. */ public function __construct() { $this->properties = ['customers' => []]; } /** * [ * 'customers' => [ * ['customerId' => 1213], * ['customerId' => 1212] * ] * ] * * @param array $properties * * @return $this */ public function addProperties(array $properties) { $this->properties['customers'] = $properties; return $this; } /** * @return array */ public function getProperties() { if (!$this->properties) { throw new \RuntimeException(); } return $this->properties; } /** * @return string */ public function getEvent() { if (!$this->event) { throw new \RuntimeException(); } return $this->event; } /** * @return \DateTimeInterface */ public function getTimestamp() { if (!$this->timestamp) { return new \DateTimeImmutable(); } return $this->timestamp; } } class EventRequest { /** * @var string */ private $providerId; /** * @var array */ private $events; /** * Constructor. */ public function __construct($providerId) { $this->providerId = $providerId; $this->events = []; } /** * @return string */ public function getProviderId() { return $this->providerId; } /** * @return array */ public function getEvents() { return $this->events; } /** * @param EventsInterface $events * * @return $this */ public function addEvents( $events) { $this->events[] = $events; return $this; } } function entity2array($entity, $recursionDepth = 2) { $result = array(); $class = new \ReflectionClass(get_class($entity)); foreach ($class->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { $methodName = $method->name; if (strpos($methodName, "get") === 0 && strlen($methodName) > 3) { $propertyName = lcfirst(substr($methodName, 3)); $value = $method->invoke($entity); if (is_array($value)) { foreach ($value as $name => $subValue) { if (is_object($subValue)) { if ($recursionDepth > 0) { $result[$propertyName][$name] = entity2array($subValue, $recursionDepth - 1); } else { $result[$propertyName][$name] = "***"; //stop recursion } continue; } if (is_array($subValue)) { $result[$propertyName][$name] = $subValue; //stop recursion continue; } $result[$propertyName][$name] = $subValue; } continue; } if (is_object($value)) { if ($recursionDepth > 0) { $result[$propertyName] = entity2array($value, $recursionDepth - 1); } else { $result[$propertyName] = "***"; //stop recursion } continue; } $result[$propertyName] = $value; } } return $result; } $foo = new EventRequest('crm_newsletter'); $event = new ReminderPushEvents(); $event->addProperties( [['customerId' => 1213], ['customerId' => 1212]]); $foo->addEvents($event); var_dump( entity2array($foo));
Output for 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
array(2) { ["providerId"]=> string(14) "crm_newsletter" ["events"]=> array(1) { [0]=> array(3) { ["properties"]=> array(1) { ["customers"]=> array(2) { [0]=> array(1) { ["customerId"]=> int(1213) } [1]=> array(1) { ["customerId"]=> int(1212) } } } ["event"]=> string(19) "reminder_push_event" ["timestamp"]=> array(4) { ["lastErrors"]=> bool(false) ["timezone"]=> string(3) "***" ["offset"]=> int(7200) ["timestamp"]=> int(1499158591) } } } }
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 array(2) { ["providerId"]=> string(14) "crm_newsletter" ["events"]=> array(1) { [0]=> array(3) { ["properties"]=> array(1) { ["customers"]=> array(2) { [0]=> array(1) { ["customerId"]=> int(1213) } [1]=> array(1) { ["customerId"]=> int(1212) } } } ["event"]=> string(19) "reminder_push_event" ["timestamp"]=> array(4) { ["lastErrors"]=> bool(false) ["timezone"]=> string(3) "***" ["offset"]=> int(7200) ["timestamp"]=> int(1499158591) } } } }
Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.25, 7.4.27 - 7.4.33, 8.0.0 - 8.0.12, 8.0.14 - 8.0.30, 8.1.0 - 8.1.28
array(2) { ["providerId"]=> string(14) "crm_newsletter" ["events"]=> array(1) { [0]=> array(3) { ["properties"]=> array(1) { ["customers"]=> array(2) { [0]=> array(1) { ["customerId"]=> int(1213) } [1]=> array(1) { ["customerId"]=> int(1212) } } } ["event"]=> string(19) "reminder_push_event" ["timestamp"]=> array(4) { ["lastErrors"]=> array(4) { ["warning_count"]=> int(0) ["warnings"]=> array(0) { } ["error_count"]=> int(0) ["errors"]=> array(0) { } } ["timezone"]=> string(3) "***" ["offset"]=> int(7200) ["timestamp"]=> int(1499158591) } } } }
Output for 7.3.32 - 7.3.33, 7.4.26, 8.0.13
array(2) { ["providerId"]=> string(14) "crm_newsletter" ["events"]=> array(1) { [0]=> array(3) { ["properties"]=> array(1) { ["customers"]=> array(2) { [0]=> array(1) { ["customerId"]=> int(1213) } [1]=> array(1) { ["customerId"]=> int(1212) } } } ["event"]=> string(19) "reminder_push_event" ["timestamp"]=> array(4) { ["lastErrors"]=> array(4) { ["warning_count"]=> int(0) ["warnings"]=> array(0) { } ["error_count"]=> int(0) ["errors"]=> array(0) { } } ["timezone"]=> string(3) "***" ["offset"]=> int(0) ["timestamp"]=> int(1499158591) } } } }

preferences:
179.42 ms | 403 KiB | 291 Q