3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php abstract class DomainEvent { private static $events = []; /** * @var \DateTime */ private $occurredOn; private function __construct() { static::addEvent($this); $this->occurredOn = new \DateTime(); } public static function trigger(...$args) { $event = new static(); $event->instantiate(...$args); return $event; } abstract protected function instantiate(...$args); private static function addEvent($event) { self::$events[] = $event; } public static function getEvents() { return self::$events; } /** * @return DateTime */ public function getOccurredOn() { return $this->occurredOn; } } class UserRegisteredEvent extends DomainEvent { private $data; protected function instantiate(...$args) { $this->data = $args; } } class User { /** * @var string */ private $username; /** * User constructor. * * @param string $username */ public function __construct($username) { $this->username = $username; UserRegisteredEvent::trigger($this); } /** * @return string */ public function getUsername() { return $this->username; } } $user = new User('Jimmy K. Oak'); $events = DomainEvent::getEvents(); var_dump($events);
based on e9Rq8
Output for 7.2.0
array(1) { [0]=> object(UserRegisteredEvent)#2 (2) { ["data":"UserRegisteredEvent":private]=> array(1) { [0]=> object(User)#1 (1) { ["username":"User":private]=> string(12) "Jimmy K. Oak" } } ["occurredOn":"DomainEvent":private]=> object(DateTime)#3 (3) { ["date"]=> string(26) "2015-10-17 22:40:55.005657" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } } }
Output for 7.1.10
array(1) { [0]=> object(UserRegisteredEvent)#2 (2) { ["data":"UserRegisteredEvent":private]=> array(1) { [0]=> object(User)#1 (1) { ["username":"User":private]=> string(12) "Jimmy K. Oak" } } ["occurredOn":"DomainEvent":private]=> object(DateTime)#3 (3) { ["date"]=> string(26) "2015-10-17 22:40:55.003253" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } } }
Output for 7.1.7
array(1) { [0]=> object(UserRegisteredEvent)#2 (2) { ["data":"UserRegisteredEvent":private]=> array(1) { [0]=> object(User)#1 (1) { ["username":"User":private]=> string(12) "Jimmy K. Oak" } } ["occurredOn":"DomainEvent":private]=> object(DateTime)#3 (3) { ["date"]=> string(26) "2015-10-17 22:40:55.004675" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } } }
Output for 7.1.6
array(1) { [0]=> object(UserRegisteredEvent)#2 (2) { ["data":"UserRegisteredEvent":private]=> array(1) { [0]=> object(User)#1 (1) { ["username":"User":private]=> string(12) "Jimmy K. Oak" } } ["occurredOn":"DomainEvent":private]=> object(DateTime)#3 (3) { ["date"]=> string(26) "2015-10-17 22:40:55.013594" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } } }
Output for 7.1.5
array(1) { [0]=> object(UserRegisteredEvent)#2 (2) { ["data":"UserRegisteredEvent":private]=> array(1) { [0]=> object(User)#1 (1) { ["username":"User":private]=> string(12) "Jimmy K. Oak" } } ["occurredOn":"DomainEvent":private]=> object(DateTime)#3 (3) { ["date"]=> string(26) "2015-10-17 22:40:55.007970" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } } }
Output for 7.1.0
array(1) { [0]=> object(UserRegisteredEvent)#2 (2) { ["data":"UserRegisteredEvent":private]=> array(1) { [0]=> object(User)#1 (1) { ["username":"User":private]=> string(12) "Jimmy K. Oak" } } ["occurredOn":"DomainEvent":private]=> object(DateTime)#3 (3) { ["date"]=> string(26) "2015-10-17 22:40:55.004103" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } } }
Output for 5.6.28, 7.0.20
array(1) { [0]=> object(UserRegisteredEvent)#2 (2) { ["data":"UserRegisteredEvent":private]=> array(1) { [0]=> object(User)#1 (1) { ["username":"User":private]=> string(12) "Jimmy K. Oak" } } ["occurredOn":"DomainEvent":private]=> object(DateTime)#3 (3) { ["date"]=> string(26) "2015-10-17 22:40:55.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } } }
Output for 7.0.6
array(1) { [0]=> object(UserRegisteredEvent)#2 (2) { ["data":"UserRegisteredEvent":private]=> array(1) { [0]=> object(User)#1 (1) { ["username":"User":private]=> string(12) "Jimmy K. Oak" } } ["occurredOn":"DomainEvent":private]=> object(DateTime)#3 (3) { ["date"]=> string(26) "2016-05-03 12:07:10.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } } }
Output for 7.0.5
array(1) { [0]=> object(UserRegisteredEvent)#2 (2) { ["data":"UserRegisteredEvent":private]=> array(1) { [0]=> object(User)#1 (1) { ["username":"User":private]=> string(12) "Jimmy K. Oak" } } ["occurredOn":"DomainEvent":private]=> object(DateTime)#3 (3) { ["date"]=> string(26) "2016-04-03 00:51:25.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } } }
Output for 7.0.4
array(1) { [0]=> object(UserRegisteredEvent)#2 (2) { ["data":"UserRegisteredEvent":private]=> array(1) { [0]=> object(User)#1 (1) { ["username":"User":private]=> string(12) "Jimmy K. Oak" } } ["occurredOn":"DomainEvent":private]=> object(DateTime)#3 (3) { ["date"]=> string(26) "2016-03-10 11:33:42.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } } }
Output for 7.0.3
array(1) { [0]=> object(UserRegisteredEvent)#2 (2) { ["data":"UserRegisteredEvent":private]=> array(1) { [0]=> object(User)#1 (1) { ["username":"User":private]=> string(12) "Jimmy K. Oak" } } ["occurredOn":"DomainEvent":private]=> object(DateTime)#3 (3) { ["date"]=> string(26) "2016-03-18 12:16:52.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } } }
Output for 7.0.2
array(1) { [0]=> object(UserRegisteredEvent)#2 (2) { ["data":"UserRegisteredEvent":private]=> array(1) { [0]=> object(User)#1 (1) { ["username":"User":private]=> string(12) "Jimmy K. Oak" } } ["occurredOn":"DomainEvent":private]=> object(DateTime)#3 (3) { ["date"]=> string(26) "2016-03-22 18:14:42.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } } }
Output for 7.0.1
array(1) { [0]=> object(UserRegisteredEvent)#2 (2) { ["data":"UserRegisteredEvent":private]=> array(1) { [0]=> object(User)#1 (1) { ["username":"User":private]=> string(12) "Jimmy K. Oak" } } ["occurredOn":"DomainEvent":private]=> object(DateTime)#3 (3) { ["date"]=> string(26) "2016-03-23 22:38:37.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } } }
Output for 7.0.0
array(1) { [0]=> object(UserRegisteredEvent)#2 (2) { ["data":"UserRegisteredEvent":private]=> array(1) { [0]=> object(User)#1 (1) { ["username":"User":private]=> string(12) "Jimmy K. Oak" } } ["occurredOn":"DomainEvent":private]=> object(DateTime)#3 (3) { ["date"]=> string(26) "2016-03-27 00:08:07.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } } }
Output for 5.6.21
array(1) { [0]=> object(UserRegisteredEvent)#2 (2) { ["data":"UserRegisteredEvent":private]=> array(1) { [0]=> object(User)#1 (1) { ["username":"User":private]=> string(12) "Jimmy K. Oak" } } ["occurredOn":"DomainEvent":private]=> object(DateTime)#3 (3) { ["date"]=> string(26) "2016-05-02 07:20:45.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } } }
Output for 5.6.20
array(1) { [0]=> object(UserRegisteredEvent)#2 (2) { ["data":"UserRegisteredEvent":private]=> array(1) { [0]=> object(User)#1 (1) { ["username":"User":private]=> string(12) "Jimmy K. Oak" } } ["occurredOn":"DomainEvent":private]=> object(DateTime)#3 (3) { ["date"]=> string(26) "2016-04-09 23:42:28.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } } }
Output for 5.6.19
array(1) { [0]=> object(UserRegisteredEvent)#2 (2) { ["data":"UserRegisteredEvent":private]=> array(1) { [0]=> object(User)#1 (1) { ["username":"User":private]=> string(12) "Jimmy K. Oak" } } ["occurredOn":"DomainEvent":private]=> object(DateTime)#3 (3) { ["date"]=> string(26) "2016-03-07 19:35:54.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } } }
Output for 5.6.18
array(1) { [0]=> object(UserRegisteredEvent)#2 (2) { ["data":"UserRegisteredEvent":private]=> array(1) { [0]=> object(User)#1 (1) { ["username":"User":private]=> string(12) "Jimmy K. Oak" } } ["occurredOn":"DomainEvent":private]=> object(DateTime)#3 (3) { ["date"]=> string(26) "2016-03-15 04:09:26.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } } }
Output for 5.6.17
array(1) { [0]=> object(UserRegisteredEvent)#2 (2) { ["data":"UserRegisteredEvent":private]=> array(1) { [0]=> object(User)#1 (1) { ["username":"User":private]=> string(12) "Jimmy K. Oak" } } ["occurredOn":"DomainEvent":private]=> object(DateTime)#3 (3) { ["date"]=> string(26) "2016-03-19 16:25:58.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } } }
Output for 5.6.16
array(1) { [0]=> object(UserRegisteredEvent)#2 (2) { ["data":"UserRegisteredEvent":private]=> array(1) { [0]=> object(User)#1 (1) { ["username":"User":private]=> string(12) "Jimmy K. Oak" } } ["occurredOn":"DomainEvent":private]=> object(DateTime)#3 (3) { ["date"]=> string(26) "2016-03-28 02:05:51.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } } }
Output for 5.6.15
array(1) { [0]=> object(UserRegisteredEvent)#2 (2) { ["data":"UserRegisteredEvent":private]=> array(1) { [0]=> object(User)#1 (1) { ["username":"User":private]=> string(12) "Jimmy K. Oak" } } ["occurredOn":"DomainEvent":private]=> object(DateTime)#3 (3) { ["date"]=> string(26) "2016-03-29 03:01:06.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } } }
Output for 5.6.14
array(1) { [0]=> object(UserRegisteredEvent)#2 (2) { ["data":"UserRegisteredEvent":private]=> array(1) { [0]=> object(User)#1 (1) { ["username":"User":private]=> string(12) "Jimmy K. Oak" } } ["occurredOn":"DomainEvent":private]=> object(DateTime)#3 (3) { ["date"]=> string(26) "2016-04-02 05:13:19.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } } }
Output for 5.6.13
array(1) { [0]=> object(UserRegisteredEvent)#2 (2) { ["data":"UserRegisteredEvent":private]=> array(1) { [0]=> object(User)#1 (1) { ["username":"User":private]=> string(12) "Jimmy K. Oak" } } ["occurredOn":"DomainEvent":private]=> object(DateTime)#3 (3) { ["date"]=> string(26) "2016-04-15 09:12:06.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } } }
Output for 5.6.12
array(1) { [0]=> object(UserRegisteredEvent)#2 (2) { ["data":"UserRegisteredEvent":private]=> array(1) { [0]=> object(User)#1 (1) { ["username":"User":private]=> string(12) "Jimmy K. Oak" } } ["occurredOn":"DomainEvent":private]=> object(DateTime)#3 (3) { ["date"]=> string(26) "2016-04-19 23:09:04.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } } }
Output for 5.6.11
array(1) { [0]=> object(UserRegisteredEvent)#2 (2) { ["data":"UserRegisteredEvent":private]=> array(1) { [0]=> object(User)#1 (1) { ["username":"User":private]=> string(12) "Jimmy K. Oak" } } ["occurredOn":"DomainEvent":private]=> object(DateTime)#3 (3) { ["date"]=> string(26) "2016-04-23 06:05:26.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } } }
Output for 5.6.10
array(1) { [0]=> object(UserRegisteredEvent)#2 (2) { ["data":"UserRegisteredEvent":private]=> array(1) { [0]=> object(User)#1 (1) { ["username":"User":private]=> string(12) "Jimmy K. Oak" } } ["occurredOn":"DomainEvent":private]=> object(DateTime)#3 (3) { ["date"]=> string(26) "2016-04-25 07:51:50.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } } }
Output for 5.6.9
array(1) { [0]=> object(UserRegisteredEvent)#2 (2) { ["data":"UserRegisteredEvent":private]=> array(1) { [0]=> object(User)#1 (1) { ["username":"User":private]=> string(12) "Jimmy K. Oak" } } ["occurredOn":"DomainEvent":private]=> object(DateTime)#3 (3) { ["date"]=> string(26) "2016-04-27 12:55:34.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } } }
Output for 5.6.8
array(1) { [0]=> object(UserRegisteredEvent)#2 (2) { ["data":"UserRegisteredEvent":private]=> array(1) { [0]=> object(User)#1 (1) { ["username":"User":private]=> string(12) "Jimmy K. Oak" } } ["occurredOn":"DomainEvent":private]=> object(DateTime)#3 (3) { ["date"]=> string(26) "2016-04-29 21:06:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } } }
Output for 5.4.2 - 5.5.35
Parse error: syntax error, unexpected '.', expecting '&' or variable (T_VARIABLE) in /in/m9F7B on line 18
Process exited with code 255.
Output for 5.3.18 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/m9F7B on line 5
Process exited with code 255.