nlopess Thu May 26 12:40:52 2005 EDT
Modified files: /phpdoc/en/language/oop5 typehinting.xml Log: add array type hinting http://cvs.php.net/diff.php/phpdoc/en/language/oop5/typehinting.xml?r1=1.2&r2=1.3&ty=u Index: phpdoc/en/language/oop5/typehinting.xml diff -u phpdoc/en/language/oop5/typehinting.xml:1.2 phpdoc/en/language/oop5/typehinting.xml:1.3 --- phpdoc/en/language/oop5/typehinting.xml:1.2 Sun Oct 3 01:07:04 2004 +++ phpdoc/en/language/oop5/typehinting.xml Thu May 26 12:40:51 2005 @@ -1,14 +1,15 @@ <?xml version="1.0" encoding="iso-8859-1"?> -<!-- $Revision: 1.2 $ --> +<!-- $Revision: 1.3 $ --> <sect1 id="language.oop5.typehinting"> <title>Type Hinting</title> <para> 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). </para> <example> - <title>Type Hinting example</title> + <title>Type Hinting examples</title> <programlisting role="php"> <![CDATA[ <?php @@ -23,6 +24,16 @@ public function test(OtherClass $otherclass) { echo $otherclass->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 @@ // 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')); ?> ]]> </programlisting> @@ -85,9 +102,9 @@ </programlisting> </example> <para> - Type Hints can only be of the <type>object</type> type. Traditional - type hinting with <type>int</type> and <type>string</type> are not - supported. + Type Hints can only be of the <type>object</type> and <type>array</type> + (since PHP 5.1) type. Traditional type hinting with <type>int</type> and + <type>string</type> isn't supported. </para> </sect1>