05-17-2009, 05:01 PM
|
#4 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
Building upon Salathe's idea, I came up with the following which may well work:
php Code:
class TalkPHP_Check { const NOT_EMPTY = '_isNotEmpty'; const IS_EMAIL = '_isEmail'; public function isValid ($szVariable) { $bValidated = false; $aArgs = func_get_args(); array_shift($aArgs); foreach ($aArgs as $szFunction) { if (! method_exists($this, $szFunction)) { throw new Exception (sprintf('Method does not exist: %s', $szFunction)); } $bValidated = $this-> $szFunction($szVariable); if (! $bValidated) { return false; } } return true; } private function _isNotEmpty ($szVariable) { if (empty($szVariable)) { return false; } return true; } private function _isEmail ($szVariable) { if (! preg_match('~^.+?@.+?\..+?$~i', $szVariable)) { return false; } return true; }}
Then of course to use that you would simply do:
php Code:
$szEmail = 'adam@example.com'; $pCheck = new TalkPHP_Check (); $bResult = $pCheck-> isValid($szEmail, TalkPHP_Check:: NOT_EMPTY, TalkPHP_Check:: IS_EMAIL); var_dump($bResult);
If you want me to explain the code, please ask! I don't know if it's understandale or not, as it is.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|