svn: /phpdoc/en/trunk/language/oop5/ typehinting.xml

From: Date: Tue, 13 Mar 2012 01:39:06 +0000
Subject: svn: /phpdoc/en/trunk/language/oop5/ typehinting.xml
Groups: php.doc.cvs 
Request: Send a blank email to doc-cvs+get-9866@lists.php.net to get a copy of this message
vrana Tue, 13 Mar 2012 01:39:06 +0000 Revision: http://svn.php.net/viewvc?view=revision&revision=324171 Log: Interface, children, callable, trait Changed paths: U phpdoc/en/trunk/language/oop5/typehinting.xml Modified: phpdoc/en/trunk/language/oop5/typehinting.xml =================================================================== --- phpdoc/en/trunk/language/oop5/typehinting.xml 2012-03-12 23:15:00 UTC (rev 324170) +++ phpdoc/en/trunk/language/oop5/typehinting.xml 2012-03-13 01:39:06 UTC (rev 324171) @@ -3,13 +3,25 @@ <sect1 xml:id="language.oop5.typehinting" xmlns="http://docbook.org/ns/docbook"> <title>Type Hinting</title> <para> - PHP 5 introduces Type Hinting. Functions are now able to force parameters + 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) or arrays (since PHP 5.1). However, if <type>NULL</type> is used + prototype), interfaces, arrays (since PHP 5.1) or <type>callable</type> + (since PHP 5.4). However, if <type>NULL</type> is used as the default parameter value, it will be allowed as an argument for any later call. </para> + <para> + If class or interface is specified as type hint then all its children or + implementations are allowed too. + </para> + + <para> + Type hints can not be used with scalar types such as <type>int</type> or + <type>string</type>. + <link linkend="language.oop5.traits">Traits</link> are not allowed either. + </para> + <example> <title>Type Hinting examples</title> <programlisting role="php"> @@ -36,6 +48,20 @@ public function test_array(array $input_array) { print_r($input_array); } + + /** + * First parameter must be iterator + */ + public function test_interface(Traversable $iterator) { + echo get_class($iterator); + } + + /** + * First parameter must be callable + */ + public function test_callable(callable $callback, $data) { + call_user_func($callback, $data); + } } // Another example class @@ -73,6 +99,12 @@ // Works: Prints the array $myclass->test_array(array('a', 'b', 'c')); + +// Works: Prints ArrayObject +$myclass->test_interface(new ArrayObject(array())); + +// Works: Prints int(1) +$myclass->test_callable('var_dump', 1); ?> ]]> </programlisting> @@ -92,13 +124,13 @@ * * First parameter must be an object of type MyClass */ -function MyFunction (MyClass $foo) { +function myFunction(MyClass $foo) { echo $foo->var; } // Works $myclass = new MyClass; -MyFunction($myclass); +myFunction($myclass); ?> ]]> </programlisting> @@ -121,11 +153,6 @@ ]]> </programlisting> </example> - <para> - 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> <!-- Keep this comment at the end of the file

« previous php.doc.cvs (#9866) next »