3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class Filters { /** * @return array */ public function toArray() { return array_map(function ($value) { return json_encode($value); }, get_object_vars($this)); } /** * @param array $arrayOfFilters * @return Filters */ public static function fromArray($arrayOfFilters) { $instance = new self; foreach ($arrayOfFilters as $key => $value) { $instance->{$key} = $value; } return $instance; } } final class UserFilters extends Filters { /** @var array */ private $registeredAtRange; /** * @param \DateTimeInterface $start * @param \DateTimeInterface|null $end */ public function setRegisteredAtRange(\DateTimeInterface $start, \DateTimeInterface $end = null) { if (is_null($end)) { $end = new \DateTime; } $this->registeredAtRange = [ 'start' => $start->format('Y-m-d H:i:s'), 'end' => $end->format('Y-m-d H:i:s'), ]; } /** * Will return 2 date time strings or null. * * @return array|null */ public function getRegisteredAtRange() { return $this->registeredAtRange; } } $a = new UserFilters; $a->setRegisteredAtRange(new DateTime('now')); $b = $a->toArray(); var_dump($b); var_dump(UserFilters::fromArray($b));
Output for 7.0.0 - 7.2.0
array(0) { } object(Filters)#3 (0) { }
Output for 5.5.0 - 5.6.28
array(0) { } object(Filters)#2 (0) { }