diff --git a/language/oop5/typehinting.xml b/language/oop5/typehinting.xml index ea85ec35fc..285759dd61 100644 --- a/language/oop5/typehinting.xml +++ b/language/oop5/typehinting.xml @@ -1,14 +1,15 @@ - + Type Hinting 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. + to be objects (by specifying the name of the class in the function + prototype) or arrays (since PHP 5.1). - Type Hinting example + Type Hinting examples var; } + + + /** + * Another test function + * + * First parameter must be an array + */ + public function test_array(array $input_array) { + print_r($input_array); + } } // Another example class @@ -54,6 +65,12 @@ $myclass->test(null); // Works: Prints Hello World $myclass->test($otherclass); + +// Fatal Error: Argument 1 must be an array +$myclass->test_array('a string'); + +// Works: Prints the array +$myclass->test_array(array('a', 'b', 'c')); ?> ]]> @@ -85,9 +102,9 @@ MyFunction($myclass); - Type Hints can only be of the object type. Traditional - type hinting with int and string are not - supported. + Type Hints can only be of the object and array + (since PHP 5.1) type. Traditional type hinting with int and + string isn't supported.