|
Posted by drtebi on 03/25/06 23:38
Hello dear fellow PHPers,
I am working on a couple classes which have functions that shall
trigger a E_USER_WARNING when one of the parameters has the wrong type.
What I am wondering about is what would be the correct way to use
trigger_error AFTER the validation failed?
Here is an example (this is a function within a class):
<?php
public function set_strlen_min($min, $error_msg = false) {
// check parameters
if (!is_int($min)) {
trigger_error('first parameter to set_strlen_min must be an
integer', E_USER_WARNING);
}
// check value
if (isset($this->value)) {
if (strlen($this->value) < $min) {
$this->error_msg = 'String must have at least ' . $min . '
characters';
parent::$error = true;
}
}
}
?>
If I pass a string to this function, PHP would convert the string to 0.
This would obviously return an unexpected result. The main problem is
that when error_reporting is set to 0, this unexpected result would not
even be noticed.
I tried to figure out what PHP's internal function do. It turns out
that this test:
<?php
$array = array('one', 'two', 'three');
$array = array_rand($array, 'string here');
var_dump($array);
?>
returns NULL, it basically killed $array.
So my question is, what is the politically correct way to do after an
error has been triggered? Should the function return nothing, return
NULL, or simply abort execution of the function?
I'd be happy to hear your opinion.
DrTebi
Navigation:
[Reply to this message]
|