假设:在 PHP 中使用参数(或不使用)。
Class myclass {
function noArgument() {
echo "no arguments<br>";
}
function oneArgument($one) {
echo "the only argument is ".$one."<br>";
}
function twoArgument($one,$two) {
echo "the first argument is ".$one." the second is ".$two."<br>";
}
}
现在,我向您展示我对上一课的测试。
$TestMyClass = new myclass();
echo "<br>Omiting arguments<br>";
$TestMyClass->twoArgument("*Lack one*");
$TestMyClass->oneArgument();
echo "<br>Excess arguments<br>";
$TestMyClass->noArgument("*With Argument*");
$TestMyClass->oneArgument("*First Argument*", "*Second Argument*");
echo "<br>End Test<br>";
结果
Omiting arguments
Warning: Missing argument 2 for myclass::twoArgument(), called in C:\...\test.php on line 4 and defined in C:\...\test.php on line 18
Notice: Undefined variable: two in C:\...\test.php on line 19
the first argument is *Lack one* the second is
Warning: Missing argument 1 for myclass::oneArgument(), called in C:\...\test.php on line 5 and defined in C:\...\test.php on line 15
Notice: Undefined variable: one in C:\...\test.php on line 16
the only argument is
Excess arguments
no arguments
the only argument is *First Argument*
End Test
我需要对多余的论点进行类似的处理!我也需要在使用不必要的参数时引发错误(或限制参数的数量)!