I’d like my functions to expect strings/integers or throw a fit, like:
warning: preg_match() expects parameter 2 to be string
However for this function
public function setImage($target, $source_path, integer $width, integer $height){...
I get:
Argument 4 passed to My_Helper_Image::setImage() must be an instance of integer, integer given
But:
function(array $expectsArray)
works as I expect, how would I achieve the same effect as with integers and strings?
Big Update
PHP 7 now supports Scalar Type Hinting
function increment(int $number) {
return $number++;
}
Scalar TypeHints are available as of PHP 7:
There is no Type Hints for scalars before PHP7. PHP 5.3.99 did have scalar typehints but it wasn’t finalised at that point if they stay and how they will work then.
Nevertheless, there is options for enforcing scalar arguments before PHP7.
There is a couple of
is_*functions that let you do that, e.g.is_int— Find whether the type of a variable is integeris_string— Find whether the type of a variable is stringTo raise a Warning, you’d use
trigger_error— Generates a user-level error/warning/notice messagewith an
E_USER_WARNINGfor$errorType.Example
Alternative
If you want to use Scalar Type Hints desperately, have a look at
which shows a technique for enforcing scalar typehints via a custom Error Handler.