Surely, when you code this
class foo {
public function bar($value) {
echo 'You called';
}
}
$obj=new foo();
$obj->bar(null); //This is okay
$obj->bar(); //This should throw an error
The result will be this when you test it.
You called
Warning: Missing argument 1 for foo::bar(), called in D:\PHP_SOURCE\tst.php on line 12 and defined in D:\PHP_SOURCE\tst.php on line 4
So you know that you have done something wrong!
But if you want to physically test for the existance of the parameter, but NULL is a valid parameter, you cannot do this as '$obj->bar();' is equivalent to $obj->bar(null);
.
So all you have to do is make the default value for the parameter be something that could never be passed in that parameter, like so
class foo {
public function bar($value='The Impossible') {
if ( $value == 'The Impossible' ) {
throw new Exception('Holy Cow Batman, The Impossible just happened');
}
echo 'You called' . PHP_EOL;
}
}
try {
$obj=new foo();
$obj->bar(null); //This is okay
$obj->bar(); //This should throw an error
}
catch (Exception $e ) {
echo $e->getMessage();
}
Which will throw the required exception.
You called
Holy Cow Batman, The Impossible just happened
Hence allowing you to catch and manage the situation if such a thing is actually possible.