Created
August 13, 2012 16:43
-
-
Save tiagodavi/3342440 to your computer and use it in GitHub Desktop.
Exemplo de switch/case com números mágicos
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
#EXEMPLO 1 (PÉSSIMO) | |
class Cliente | |
{ | |
var $tipo; | |
} | |
function redireciona(Cliente $cliente){ | |
switch($cliente->tipo){ | |
case 1: | |
header('Location: tela1.php'); | |
break; | |
case 2: | |
header('Location: tela2.php'); | |
break; | |
case 3: | |
header('Location: tela3.php'); | |
break; | |
default: | |
die('Sem tipo - Entre em contato com o administrador'); | |
} | |
} | |
$cliente = new Cliente; | |
$cliente->tipo = 1; | |
redireciona($cliente); | |
#EXEMPLO 2 (SAUDÁVEL) | |
class Cliente | |
{ | |
private $tipo; | |
const VIP = 1; | |
const COMUM = 2; | |
const ESPECIAL = 3; | |
public function setTipo($tipo) | |
{ | |
$this->tipo = $tipo; | |
} | |
public function getTipo() | |
{ | |
return $this->tipo; | |
} | |
} | |
redireciona(Cliente $cliente) | |
{ | |
switch($cliente->getTipo()){ | |
case $cliente::VIP: | |
header('Location: cliente_vip.php'); | |
break; | |
case $cliente::COMUM: | |
header('Location: cliente_comum.php'); | |
break; | |
case $cliente::ESPECIAL: | |
header('Location: cliente_especial.php'); | |
break; | |
default: | |
die('Sem tipo - Entre em contato com o administrador'); | |
} | |
} | |
$cliente = new Cliente(); | |
$cliente->setTipo($cliente::VIP); | |
redireciona($cliente); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment