3v4l.org

run code in 300+ PHP versions simultaneously
<?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 git.master, git.master_jit, rfc.property-hooks
1800

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
62.61 ms | 401 KiB | 8 Q