Source of: /manual/en/language.pseudo-types.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/language.types.inc";
$setup = array (
'home' =>
array (
0 => 'index.php',
1 => 'PHP Manual',
),
'head' =>
array (
0 => 'UTF-8',
1 => 'en',
),
'this' =>
array (
0 => 'language.pseudo-types.php',
1 => 'Pseudo-types and variables used in this documentation',
),
'up' =>
array (
0 => 'language.types.php',
1 => 'Types',
),
'prev' =>
array (
0 => 'language.types.null.php',
1 => 'NULL',
),
'next' =>
array (
0 => 'language.types.type-juggling.php',
1 => 'Type Juggling',
),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);
manual_header();
?>
<div id="language.pseudo-types" class="sect1">
<h2 class="title">Pseudo-types and variables used in this documentation</h2>
<div id="language.types.mixed" class="sect2">
<h3 class="title">mixed</h3>
<p class="para">
<i>mixed</i> indicates that a parameter may accept multiple (but
not necessarily all) types.
</p>
<p class="para">
<a href="function.gettype.php" class="function">gettype()</a> for example will accept all PHP types, while
<a href="function.str-replace.php" class="function">str_replace()</a> will accept <a href="language.types.string.php" class="type string">string</a>s and
<a href="language.types.array.php" class="type array">array</a>s.
</p>
</div>
<div id="language.types.number" class="sect2">
<h3 class="title">number</h3>
<p class="para">
<i>number</i> indicates that a parameter can be either
<a href="language.types.integer.php" class="type integer">integer</a> or <a href="language.types.float.php" class="type float">float</a>.
</p>
</div>
<div id="language.types.callback" class="sect2">
<h3 class="title">callback</h3>
<p class="para">
Some functions like <a href="function.call-user-func.php" class="function">call_user_func()</a> or
<a href="function.usort.php" class="function">usort()</a> accept user-defined callback functions as a
parameter. Callback functions can not only be simple functions, but also
<a href="language.types.object.php" class="type object">object</a> methods, including static class methods.
</p>
<p class="para">
A PHP function is passed by its name as a <a href="language.types.string.php" class="type string">string</a>. Any built-in
or user-defined function can be used, except language constructs such as:
<a href="function.array.php" class="function">array()</a>, <a href="function.echo.php" class="function">echo()</a>,
<a href="function.empty.php" class="function">empty()</a>, <a href="function.eval.php" class="function">eval()</a>,
<a href="function.exit.php" class="function">exit()</a>, <a href="function.isset.php" class="function">isset()</a>,
<a href="function.list.php" class="function">list()</a>, <a href="function.print.php" class="function">print()</a> or
<a href="function.unset.php" class="function">unset()</a>.
</p>
<p class="para">
A method of an instantiated <a href="language.types.object.php" class="type object">object</a> is passed as an
<a href="language.types.array.php" class="type array">array</a> containing an <a href="language.types.object.php" class="type object">object</a> at index 0 and the
method name at index 1.
</p>
<p class="para">
Static class methods can also be passed without instantiating an
<a href="language.types.object.php" class="type object">object</a> of that class by passing the class name instead of an
<a href="language.types.object.php" class="type object">object</a> at index 0.
</p>
<p class="para">
Apart from common user-defined function, <a href="function.create-function.php" class="function">create_function()</a>
can also be used to create an anonymous callback function. As of PHP 5.3.0
it is possible to also pass a
<a href="functions.anonymous.php" class="link">closure</a> to a callback parameter.
</p>
<p class="para">
</p><div class="example">
<p><b>Example #1
Callback function examples
</b></p>
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php <br /><br /></span><span style="color: #FF8000">// An example callback function<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">my_callback_function</span><span style="color: #007700">() {<br /> echo </span><span style="color: #DD0000">'hello world!'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">// An example callback method<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">MyClass </span><span style="color: #007700">{<br /> static function </span><span style="color: #0000BB">myCallbackMethod</span><span style="color: #007700">() {<br /> echo </span><span style="color: #DD0000">'Hello World!'</span><span style="color: #007700">;<br /> }<br />}<br /><br /></span><span style="color: #FF8000">// Type 1: Simple callback<br /></span><span style="color: #0000BB">call_user_func</span><span style="color: #007700">(</span><span style="color: #DD0000">'my_callback_function'</span><span style="color: #007700">); <br /><br /></span><span style="color: #FF8000">// Type 2: Static class method call<br /></span><span style="color: #0000BB">call_user_func</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'MyClass'</span><span style="color: #007700">, </span><span style="color: #DD0000">'myCallbackMethod'</span><span style="color: #007700">)); <br /><br /></span><span style="color: #FF8000">// Type 3: Object method call<br /></span><span style="color: #0000BB">$obj </span><span style="color: #007700">= new </span><span style="color: #0000BB">MyClass</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">call_user_func</span><span style="color: #007700">(array(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">, </span><span style="color: #DD0000">'myCallbackMethod'</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">// Type 4: Static class method call (As of PHP 5.2.3)<br /></span><span style="color: #0000BB">call_user_func</span><span style="color: #007700">(</span><span style="color: #DD0000">'MyClass::myCallbackMethod'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Type 5: Relative static class method call (As of PHP 5.3.0)<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">A </span><span style="color: #007700">{<br /> public static function </span><span style="color: #0000BB">who</span><span style="color: #007700">() {<br /> echo </span><span style="color: #DD0000">"A\n"</span><span style="color: #007700">;<br /> }<br />}<br /><br />class </span><span style="color: #0000BB">B </span><span style="color: #007700">extends </span><span style="color: #0000BB">A </span><span style="color: #007700">{<br /> public static function </span><span style="color: #0000BB">who</span><span style="color: #007700">() {<br /> echo </span><span style="color: #DD0000">"B\n"</span><span style="color: #007700">;<br /> }<br />}<br /><br /></span><span style="color: #0000BB">call_user_func</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'B'</span><span style="color: #007700">, </span><span style="color: #DD0000">'parent::who'</span><span style="color: #007700">)); </span><span style="color: #FF8000">// A<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
</div><p>
</p>
<p class="para">
</p><div class="example">
<p><b>Example #2
Callback example using a Closure
</b></p>
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// Our closure<br /></span><span style="color: #0000BB">$double </span><span style="color: #007700">= function(</span><span style="color: #0000BB">$a</span><span style="color: #007700">) {<br /> return </span><span style="color: #0000BB">$a </span><span style="color: #007700">* </span><span style="color: #0000BB">2</span><span style="color: #007700">;<br />};<br /><br /></span><span style="color: #FF8000">// This is our range of numbers<br /></span><span style="color: #0000BB">$numbers </span><span style="color: #007700">= </span><span style="color: #0000BB">range</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">5</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Use the closure as a callback here to <br />// double the size of each element in our <br />// range<br /></span><span style="color: #0000BB">$new_numbers </span><span style="color: #007700">= </span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #0000BB">$double</span><span style="color: #007700">, </span><span style="color: #0000BB">$numbers</span><span style="color: #007700">);<br /><br />print </span><span style="color: #0000BB">implode</span><span style="color: #007700">(</span><span style="color: #DD0000">' '</span><span style="color: #007700">, </span><span style="color: #0000BB">$new_numbers</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
<div class="example-contents para"><p>The above example will output:</p></div>
<div class="example-contents screen">
<div class="cdata"><pre>
2 4 6 8 10
</pre></div>
</div>
</div><p>
</p>
<blockquote><p><b class="note">Note</b>:
<span class="simpara">
In PHP4, it was necessary to use a reference to create a callback that
points to the actual <a href="language.types.object.php" class="type object">object</a>, and not a copy of it. For more
details, see
<a href="language.references.php" class="link">References Explained</a>.
</span>
</p></blockquote>
</div>
<div id="language.types.void" class="sect2">
<h3 class="title">void</h3>
<p class="para">
<i>void</i> as a return type means that the return value is
useless. <i>void</i> in a parameter list means that the function
doesn't accept any parameters.
</p>
</div>
<div id="language.types.dotdotdot" class="sect2">
<h3 class="title">...</h3>
<p class="para">
<i><tt class="parameter">$...</tt></i>
in function prototypes means
<i>and so on</i>. This variable name is used when a function can
take an endless number of arguments.
</p>
</div>
</div><?php manual_footer(); ?>