From 784252b7845d7054ae190cb2b7410ccb87eda110 Mon Sep 17 00:00:00 2001 From: Nuno Lopes Date: Thu, 26 May 2005 16:40:51 +0000 Subject: [PATCH] add array type hinting git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@187031 c90b9560-bf6c-de11-be94-00142212c4b1 --- language/oop5/typehinting.xml | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) 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.