3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php class Logger { public function log($data) { // Actually log your data echo 'LOGGING DATA: ' . $data . PHP_EOL; } } class Hassan { public function multiplyByTwo($i) { // Doing business logic $i = (int)$i * 2; return $i; } } class HassanLoggerDecorator { protected $hassan; protected $logger; public function __construct(Hassan $hassan, Logger $logger) { $this->hassan = $hassan; $this->logger = $logger; } public function __call($method, $args) { $this->logger->log(sprintf("Calling method: %s with args: %s", $method, print_r($args, true))); $result = call_user_func_array( array($this->hassan, $method), $args ); $this->logger->log(sprintf("Got result: %s", $result)); return $result; } } // Now you can use your Hassan object without logging... $hassan = new Hassan; echo "3 multipled by two is: " . $hassan->multiplyByTwo(3) . '\n\r'; // Now let's log the output of Hassan, without having it as a dependency!! (Never FORCE logging in anything you do, it's ALWAYS optional) $hassanWithLoggingFunctionality = new HassanLoggerDecorator($hassan, new Logger); echo $hassanWithLoggingFunctionality->multiplyByTwo(3);
based on R5AWA
Output for 5.0.2 - 5.6.21, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
3 multipled by two is: 6\n\rLOGGING DATA: Calling method: multiplyByTwo with args: Array ( [0] => 3 ) LOGGING DATA: Got result: 6 6
Output for 5.0.0 - 5.0.1
3 multipled by two is: 6\n\r Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/Qf1qp on line 8 LOGGING DATA: Calling method: multiplyByTwo with args: Array ( [0] => 3 ) PHP_EOL Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/Qf1qp on line 8 LOGGING DATA: Got result: 6PHP_EOL6
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/Qf1qp on line 5
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/Qf1qp on line 5
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/Qf1qp on line 5
Process exited with code 255.