mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-16 00:48:54 +00:00
add array type hinting
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@187031 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
parent
8b49b2e359
commit
784252b784
1 changed files with 23 additions and 6 deletions
|
@ -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 @@ class MyClass
|
|||
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 @@ $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'));
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
|
@ -85,9 +102,9 @@ MyFunction($myclass);
|
|||
</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>
|
||||
|
||||
|
|
Loading…
Reference in a new issue