I'd like to know whether one can type-hint a method to expect primitive types?
Something like this:
public function someMethod(string $str)
//^^^^^^
Or:
private function anotherMethod(int $num)
//^^^
the same way you would:
private function otherMethod(Person $rambo)
//^^^^^^
Is that possible in php?
In PHP 7 they added the following:
Type declarations allow functions to require that parameters are of a certain type at call time. If the given value is of the incorrect type, then an error is generated: in PHP 5, this will be a recoverable fatal error, while PHP 7 will throw a TypeError exception.
Reference: http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration
When this answer was asked, PHP 5 was the latest and said the following:
PHP 5 introduces type hinting. Functions are now able to force parameters to be objects (by specifying the name of the class in the function prototype), interfaces, arrays (since PHP 5.1) or callable (since PHP 5.4). However, if NULL is used as the default parameter value, it will be allowed as an argument for any later call.
If class or interface is specified as type hint then all its children or implementations are allowed too.
Type hints cannot be used with scalar types such as int or string. Resources and Traits are not allowed either.
Reference: http://php.net/manual/en/language.oop5.typehinting.php