- ctype_digit: documentation ( source)
<?php
class User
{
const STATE_BLOCKED = 0;
const STATE_VALIDATED = 1;
const STATE_NOT_VALIDATED = 2;
public static $states = [
User::STATE_BLOCKED => 'Bloqueado',
User::STATE_VALIDATED => 'Normal',
User::STATE_NOT_VALIDATED => 'Pendiente de validaciĆ³n',
];
public function setState($state)
{
if (!ctype_digit($state) or !array_key_exists($state, User::$states)) {
throw new \InvalidArgumentException("bad state '$state'");
}
echo 'ok';
}
public function setUnvalidated()
{
$this->setState(User::STATE_NOT_VALIDATED);
}
}
$u = new User();
$u->setUnvalidated();