3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php abstract class RegraDeCalculo { public function calcula(Funcionario $funcionario) { $salario = $funcionario->getSalario(); if ($salario > $this->limite()) { return $salario * $this->porcentagemAcimaDoLimite(); } return $salario * $this->porcentagemBase(); } protected function limite(){} protected function porcentagemAcimaDoLimite(){} protected function porcentagemBase(){} } class DezOuVintePorCento extends RegraDeCalculo { protected function porcentagemBase() { return 0.9; } protected function porcentagemAcimaDoLimite() { return 0.8; } protected function limite() { return 3000; } } class QuinzeOuVinteECincoPorCento extends RegraDeCalculo { protected function porcentagemBase() { return 0.85; } protected function porcentagemAcimaDoLimite() { return 0.75; } protected function limite() { return 2500; } } class Funcionario { protected $nome; protected $salario; protected $cargo; public function __construct($nome, $salario, $cargo) { $this->nome = $nome; $this->salario = $salario; $this->cargo = $cargo; } public function getNome() { return $this->nome; } public function getSalario() { return $this->salario; } public function getCargo() { return $this->cargo; } } class Cargo { private $cargos = array( // Removidos namespaces somente para os exemplos "desenvolvedor" => "DezOuVintePorCento", "dba" => "QuinzeOuVinteECincoPorCento", "testador" => "QuinzeOuVinteECincoPorCento" ); private $regra; public function __construct($regra) { if (array_key_exists($regra, $this->cargos)) { $this->regra = $this->cargos[$regra]; } else { throw new \Exception("Cargo inválido"); } } public function getRegra() { return new $this->regra(); } } $cargo = new Cargo('desenvolvedor'); $calculo = $cargo->getRegra()->calcula(new Funcionario('Andre', 2000.0, 'desenvolvedor')); echo $calculo;
Output for 5.3.0 - 7.2.0
1800
Output for 5.0.0 - 5.2.17
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/cFlQo on line 91 1800
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_CLASS in /in/cFlQo on line 3
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_CLASS in /in/cFlQo on line 3
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/cFlQo on line 3
Process exited with code 255.