1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 297078 $ -->
<sect2 xml:id="internals2.ze1.zendapi.returning" xmlns="http://docbook.org/ns/docbook">
<title>Returning Values</title>
<para>
Returning values from your functions to PHP was described briefly
in an earlier section; this section gives the details. Return
values are passed via the <envar>return_value</envar> variable,
which is passed to your functions as argument. The
<envar>return_value</envar> argument consists of a
<envar>zval</envar> container (see the earlier discussion of the
call interface) that you can freely modify. The container itself is
already allocated, so you don't have to run
<literal>MAKE_STD_ZVAL</literal> on it. Instead, you can access its
members directly.
</para>
<para>
To make returning values from functions easier and to prevent
hassles with accessing the internal structures of the
<envar>zval</envar> container, a set of predefined macros is
available (as usual). These macros automatically set the
correspondent type and value, as described in
<xref linkend="internals2.ze1.zendapi.tab.return"/> and <xref linkend="internals2.ze1.zendapi.tab.retval"/>.
</para>
<note>
<para>
The macros in <xref linkend="internals2.ze1.zendapi.tab.return"/> automatically
<emphasis>return</emphasis> from your function, those in
<xref linkend="internals2.ze1.zendapi.tab.retval"/> only <emphasis>set</emphasis>
the return value; they don't return from your function.
</para>
</note>
<table xml:id="internals2.ze1.zendapi.tab.return">
<title>Predefined Macros for Returning Values from a
Function</title>
<tgroup cols="2">
<colspec colnum="1" colname="col1" colwidth="*"/>
<colspec colnum="2" colname="col2" colwidth="*"/>
<tbody>
<row>
<entry colname="col1">Macro</entry>
<entry colname="col2">Description</entry>
</row>
<row>
<entry colname="col1"><literal>RETURN_RESOURCE(resource)</literal></entry>
<entry colname="col2">Returns a resource.</entry>
</row>
<row>
<entry colname="col1"><literal>RETURN_BOOL(bool)</literal></entry>
<entry colname="col2">Returns a Boolean.</entry>
</row>
<row>
<entry colname="col1"><literal>RETURN_NULL()</literal></entry>
<entry colname="col2">Returns nothing (a NULL value).</entry>
</row>
<row>
<entry colname="col1"><literal>RETURN_LONG(long)</literal></entry>
<entry colname="col2">Returns a long.</entry>
</row>
<row>
<entry colname="col1"><literal>RETURN_DOUBLE(double)</literal></entry>
<entry colname="col2">Returns a double.</entry>
</row>
<row>
<entry colname="col1">
<literal>RETURN_STRING(string, duplicate)</literal>
</entry>
<entry colname="col2">
Returns a string. The <envar>duplicate</envar> flag indicates
whether the string should be duplicated using
<function>estrdup</function>.
</entry>
</row>
<row>
<entry colname="col1">
<literal>RETURN_STRINGL(string, length, duplicate)</literal>
</entry>
<entry colname="col2">
Returns a string of the specified length; otherwise, behaves
like <literal>RETURN_STRING</literal>. This macro is faster
and binary-safe, however.
</entry>
</row>
<row>
<entry colname="col1"><literal>RETURN_EMPTY_STRING()</literal></entry>
<entry colname="col2">Returns an empty string.</entry>
</row>
<row>
<entry colname="col1"><literal>RETURN_FALSE</literal></entry>
<entry colname="col2">Returns Boolean false.</entry>
</row>
<row>
<entry colname="col1"><literal>RETURN_TRUE</literal></entry>
<entry colname="col2">Returns Boolean true.</entry>
</row>
</tbody>
</tgroup>
</table>
<table xml:id="internals2.ze1.zendapi.tab.retval">
<title>Predefined Macros for Setting the Return Value
of a Function</title>
<tgroup cols="2">
<colspec colnum="1" colname="col1" colwidth="*"/>
<colspec colnum="2" colname="col2" colwidth="*"/>
<tbody>
<row>
<entry colname="col1">Macro</entry>
<entry colname="col2">Description</entry>
</row>
<row>
<entry colname="col1"><literal>RETVAL_RESOURCE(resource)</literal></entry>
<entry colname="col2">Sets the return value to the specified
resource.</entry>
</row>
<row>
<entry colname="col1"><literal>RETVAL_BOOL(bool)</literal></entry>
<entry colname="col2">Sets the return value to the specified
Boolean value.</entry>
</row>
<row>
<entry colname="col1"><literal>RETVAL_NULL</literal></entry>
<entry colname="col2">Sets the return value to NULL.</entry>
</row>
<row>
<entry colname="col1"><literal>RETVAL_LONG(long)</literal></entry>
<entry colname="col2">
Sets the return value to the specified long.
</entry>
</row>
<row>
<entry colname="col1"><literal>RETVAL_DOUBLE(double)</literal></entry>
<entry colname="col2">
Sets the return value to the specified double.
</entry>
</row>
<row>
<entry colname="col1">
<literal>RETVAL_STRING(string, duplicate)</literal>
</entry>
<entry colname="col2">
Sets the return value to the specified string and duplicates
it to Zend internal memory if desired (see also
<literal>RETURN_STRING</literal>).
</entry>
</row>
<row>
<entry colname="col1">
<literal>RETVAL_STRINGL(string, length, duplicate)</literal>
</entry>
<entry colname="col2">
Sets the return value to the specified string and forces the
length to become <envar>length</envar> (see also
<literal>RETVAL_STRING</literal>). This macro is faster and
binary-safe, and should be used whenever the string length is
known.
</entry>
</row>
<row>
<entry colname="col1">
<literal>RETVAL_EMPTY_STRING</literal>
</entry>
<entry colname="col2">
Sets the return value to an empty string.
</entry>
</row>
<row>
<entry colname="col1"><literal>RETVAL_FALSE</literal></entry>
<entry colname="col2">
Sets the return value to Boolean false.
</entry>
</row>
<row>
<entry colname="col1"><literal>RETVAL_TRUE</literal></entry>
<entry colname="col2">
Sets the return value to Boolean true.
</entry>
</row>
</tbody>
</tgroup>
</table>
<para>
Complex types such as arrays and objects can be returned by using
<function>array_init</function> and
<function>object_init</function>, as well as the corresponding hash
functions on <envar>return_value</envar>. Since these types cannot
be constructed of trivial information, there are no predefined
macros for them.
</para>
</sect2>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
|