With the below code, $quantity is assumed to be an integer but I’m not doing any checking against it to require it to be so.
public function addProduct($product, $quantity) {
The below code will require it to be an integer, BUT if $quantity = '1'; it’ll fail because it’s a string. Is it possible for me to force $quantity to come through as an integer in this function, or do I HAVE to do $object->addProduct($product, (int) $quantity); ?
public function addProduct($product, int $quantity) {
Lastly, is it possible for me to flag $product as either a string or an integer, but if it’s passed an object it’ll break (without writing an is_object() check)
Type hinting is not available for primitive type. The only solution you have in this case is to use intval() or is_int() on your param :