Exceptions PHP5

Eléphanteau du PHP | 45 Messages

22 sept. 2008, 23:52

Bonjour/Bonsoir à tous,

Je dois avouer que je n'ai pas compris quelque chose dans les Exceptions de PHP5. Si vous pouviez éclairer ma lanterne.

Soit,
/**
* Convert file's size in O, Ko, Mo
*
* @param  : $size as INTEGER -> int to convert
* @param  : $unity as STRING -> o | Ko | Mo
* @return : $answers as STRING -> $size . ' o' | $size . ' Ko' | $size . ' Mo'
*/
function getSizeConvert($size, $unity) {
if(!is_int($size)) {
	throw new Exception ('Invalid argument size (1) : ' . $size . ', must be int');
}
if($unity !== "o") {
	throw new Exception ('Invalid argument unity (2) : ' . $unity . ', must be \'o\', \'Ko\', \'Mo\'');
}
// bla bla
}
Je voudrais savoir comment tester l'argument numéro 2 (soit o, Ko, Mo) sans REGEX.Car lorsque j'écris cela.
	function getSizeConvert($size, $unity) {
	    if(!is_int($size)) {
		    throw new Exception ('Invalid argument size (1) : ' . $size . ', must be int');
		}
	    if($unity !== "o" || $unity !== "Ko") {
		    throw new Exception ('Invalid argument unity (2) : ' . $unity . ', must be \'o\', \'Ko\', \'Mo\'');
		}
	}
J'ai un retour Exception bien qu'ayant mis les arguments comme il faut.

Honte sur moi ! Question stupide.
	function getSizeConvert($size, $unity) {
	    if(!is_int($size)) {
		    throw new Exception ('Invalid argument size (1) : ' . $size . ', must be int');
		}
	    if($unity !== "o" [color=red]&&[/color] $unity !== "Ko") {
		    throw new Exception ('Invalid argument unity (2) : ' . $unity . ', must be \'o\', \'Ko\', \'Mo\'');
		}
	}
Faut d'inattention.