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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
|
<?xml version="1.0" encoding="iso-8859-2"?>
<reference id="ref.funchand">
<title>Funkce pro prci s funkcemi</title>
<titleabbrev>Funkce</titleabbrev>
<partintro>
<para>
Vechny tyto funkce pokrvaj rzn aspekty prce s funkcemi.
</para>
</partintro>
<refentry id="function.call-user-func">
<refnamediv>
<refname>call_user_func</refname>
<refpurpose>Zavolat uivatelskou funkci urenou prvnm argumentem</refpurpose>
</refnamediv>
<refsect1>
<title>Popis</title>
<methodsynopsis>
<type>mixed</type><methodname>call_user_func</methodname>
<methodparam><type>string</type><parameter>function_name</parameter></methodparam>
<methodparam choice="opt"><type>mixed</type><parameter>parameter</parameter></methodparam>
<methodparam choice="opt"><type>mixed</type><parameter>...</parameter></methodparam>
</methodsynopsis>
<para>
Zavol uivatelsky definouvanou funkci urenou argumentem
<parameter>function_name</parameter>. Zvate nsledujc ukzku:
<informalexample>
<programlisting role="php">
function barber ($type) {
print "You wanted a $type haircut, no problem";
}
call_user_func ('barber', "mushroom");
call_user_func ('barber', "shave");
</programlisting>
</informalexample>
</para>
</refsect1>
</refentry>
<refentry id="function.create-function">
<refnamediv>
<refname>create_function</refname>
<refpurpose>Vytvoit anonymn (lambda-style) funkci</refpurpose>
</refnamediv>
<refsect1>
<title>Popis</title>
<methodsynopsis>
<type>string</type><methodname>create_function</methodname>
<methodparam><type>string</type><parameter>args</parameter></methodparam>
<methodparam><type>string</type><parameter>code</parameter></methodparam>
</methodsynopsis>
<para>
Z pedanch argument vytvo anonymn funkci, a vrt uniktn nzev tto
funkce. <parameter>args</parameter> se obvykle pedv jako string v
jednoduchch uvozovkch, a doporuujeme to i pro <parameter>code</parameter>.
Dvodem pro jednoduch uvozovky je ochrana nzv promnnch ped parsovnm,
pokud pouijete dvojit uvozovky, budete muset oescapovat nzvy promnnch,
nap. <literal>\$avar</literal>.
</para>
<para>
Tuto funkci mete (napklad) pout k vytvoen funkce z dat shromdnch
za bhu programu:
<example>
<title>
Vytvoen anonymn funkce pomoc <function>create_function</function>
</title>
<programlisting role="php">
$newfunc = create_function('$a,$b','return "ln($a) + ln($b) = ".log($a * $b);');
echo "New anonymous function: $newfunc\n";
echo $newfunc(2,M_E)."\n";
// vstup
// New anonymous function: lambda_1
// ln(2) + ln(2.718281828459) = 1.6931471805599
</programlisting>
</example>
Nebo teba k vytvoen obecnho handleru, kter na pedan argumenty
aplikuje sadu operac:
<example>
<title>
Vytvoen obecn zpracujc funkce pomoc
<function>create_function</function>
</title>
<programlisting role="php">
function process($var1, $var2, $farr) {
for ($f=0; $f < count($farr); $f++)
echo $farr[$f]($var1,$var2)."\n";
}
// create a bunch of math functions
$f1 = 'if ($a >=0) {return "b*a^2 = ".$b*sqrt($a);} else {return false;}';
$f2 = "return \"min(b^2+a, a^2,b) = \".min(\$a*\$a+\$b,\$b*\$b+\$a);";
$f3 = 'if ($a > 0 && $b != 0) {return "ln(a)/b = ".log($a)/$b;} else {return false;}';
$farr = array(
create_function('$x,$y', 'return "some trig: ".(sin($x) + $x*cos($y));'),
create_function('$x,$y', 'return "a hypotenuse: ".sqrt($x*$x + $y*$y);'),
create_function('$a,$b', $f1),
create_function('$a,$b', $f2),
create_function('$a,$b', $f3)
);
echo "\nUsing the first array of anonymous functions\n";
echo "parameters: 2.3445, M_PI\n";
process(2.3445, M_PI, $farr);
// now make a bunch of string processing functions
$garr = array(
create_function('$b,$a','if (strncmp($a,$b,3) == 0) return "** \"$a\" '.
'and \"$b\"\n** Look the same to me! (looking at the first 3 chars)";'),
create_function('$a,$b','; return "CRCs: ".crc32($a)." , ".crc32(b);'),
create_function('$a,$b','; return "similar(a,b) = ".similar_text($a,$b,&$p)."($p%)";')
);
echo "\nUsing the second array of anonymous functions\n";
process("Twas brilling and the slithy toves", "Twas the night", $garr);
</programlisting>
</example>
kdy spustte veuveden kd, vstup bude:
<informalexample>
<programlisting>
Using the first array of anonymous functions
parameters: 2.3445, M_PI
some trig: -1.6291725057799
a hypotenuse: 3.9199852871011
b*a^2 = 4.8103313314525
min(b^2+a, a^2,b) = 8.6382729035898
ln(a/b) = 0.27122299212594
Using the second array of anonymous functions
** "Twas the night" and "Twas brilling and the slithy toves"
** Look the same to me! (looking at the first 3 chars)
CRCs: -725381282 , 1908338681
similar(a,b) = 11(45.833333333333%)
</programlisting>
</informalexample>
Ale zejm nejbnjm vyuitm lambda-style (anonymnch) funkc je tvorba
callback funkc, nap. pro pouit v <function>array_walk</function> nebo
<function>usort</function>
<example>
<title>Vyuit anonymnch funkc jako callback funkc</title>
<programlisting role="php">
$av = array("the ","a ","that ","this ");
array_walk($av, create_function('&$v,$k','$v = $v."mango";'));
print_r($av); // for PHP 3 use var_dump()
// outputs:
// Array
// (
// [0] => the mango
// [1] => a mango
// [2] => that mango
// [3] => this mango
// )
// an array of strings ordered from shorter to longer
$sv = array("small","larger","a big string","it is a string thing");
print_r($sv);
// outputs:
// Array
// (
// [0] => small
// [1] => larger
// [2] => a big string
// [3] => it is a string thing
// )
// sort it from longer to shorter
usort($sv, create_function('$a,$b','return strlen($b) - strlen($a);'));
print_r($sv);
// outputs:
// Array
// (
// [0] => it is a string thing
// [1] => a big string
// [2] => larger
// [3] => small
// )
</programlisting>
</example>
</para>
</refsect1>
</refentry>
<refentry id="function.func-get-arg">
<refnamediv>
<refname>func_get_arg</refname>
<refpurpose>Vrtit poloku ze seznamu argument</refpurpose>
</refnamediv>
<refsect1>
<title>Popis</title>
<methodsynopsis>
<type>mixed</type><methodname>func_get_arg</methodname>
<methodparam><type>int</type><parameter>arg_num</parameter></methodparam>
</methodsynopsis>
<simpara>
Vrac argument, kter je na
<parameter>arg_num</parameter>-t pozici v seznamu argument uiavtelsky
definovan funkce. Argumenty funkc se potaj od nuly.
<function>func_get_arg</function> pi voln mimo definici funkce generuje
varovn.
</simpara>
<simpara>
Pokud je <parameter>arg_num</parameter> vt ne poet skuten pedanch
argument, vygeneruje varovn a vrt &false;.
</simpara>
<para>
<informalexample>
<programlisting role="php">
<?php
function foo() {
$numargs = func_num_args();
echo "Number of arguments: $numargs<br>\n";
if ($numargs >= 2) {
echo "Second argument is: " . func_get_arg (1) . "<br>\n";
}
}
foo (1, 2, 3);
?>
</programlisting>
</informalexample>
</para>
<simpara>
<function>func_get_arg</function> se d pout ve spojen s
<function>func_num_args</function> a
<function>func_get_args</function> k tvorb uivatelsky definovanch
funkc, kter pijmaj promnn poet argument.
</simpara>
<note>
<simpara>
Tato funkce byla pidna v PHP 4.
</simpara>
</note>
</refsect1>
</refentry>
<refentry id="function.func-get-args">
<refnamediv>
<refname>func_get_args</refname>
<refpurpose>Vrtit pole obsahujc seznam argument funkce</refpurpose>
</refnamediv>
<refsect1>
<title>Popis</title>
<methodsynopsis>
<type>array</type><methodname>func_get_args</methodname>
<methodparam><type>void</type><parameter/></methodparam>
</methodsynopsis>
<simpara>
Vrac pole, jeho kad prvek je odpovdajc polokou seznamu argument
souasn uivatelsky definovan funkce. <function>func_get_args</function>
pi voln mimo definici funkce generuje varovn.
</simpara>
<para>
<informalexample>
<programlisting role="php">
<?php
function foo() {
$numargs = func_num_args();
echo "Number of arguments: $numargs<br>\n";
if ($numargs >= 2) {
echo "Second argument is: " . func_get_arg (1) . "<br>\n";
}
$arg_list = func_get_args();
for ($i = 0; $i < $numargs; $i++) {
echo "Argument $i is: " . $arg_list[$i] . "<br>\n";
}
}
foo (1, 2, 3);
?>
</programlisting>
</informalexample>
</para>
<simpara>
<function>func_get_args</function> se d pout ve spojen s
<function>func_num_args</function> a <function>func_get_arg</function> k
tvorb uivatelsky definovanch funkc, kter pijmaj promnny poet
argument.
</simpara>
<note>
<simpara>
Tato funkce byla pidna v PHP 4.
</simpara>
</note>
</refsect1>
</refentry>
<refentry id="function.func-num-args">
<refnamediv>
<refname>func_num_args</refname>
<refpurpose>Vrtit poet argument pedanch funkci</refpurpose>
</refnamediv>
<refsect1>
<title>Popis</title>
<methodsynopsis>
<type>int</type><methodname>func_num_args</methodname>
<methodparam><type>void</type><parameter/></methodparam>
</methodsynopsis>
<simpara>
Vrac poet argument pedanch souasn uivatelsky definovan funkci.
<function>func_num_args</function> pri voln mimo definici funkce generuje
warning.
definition.
</simpara>
<para>
<informalexample>
<programlisting role="php">
<?php
function foo() {
$numargs = func_num_args();
echo "Number of arguments: $numargs\n";
}
foo (1, 2, 3); // Prints 'Number of arguments: 3'
?>
</programlisting>
</informalexample>
</para>
<simpara>
<function>func_num_args</function> se d pouit ve spojen s
<function>func_get_arg</function> a <function>func_get_args</function> k
tvorb uivatelsky definovanch funkc, kter pijmaj promnn poet
argument.
</simpara>
<note>
<simpara>
Tato funkce byla pidna v PHP 4.
</simpara>
</note>
</refsect1>
</refentry>
<refentry id="function.function-exists">
<refnamediv>
<refname>function_exists</refname>
<refpurpose>Vrtit &true;, pokud je dan funkce definovna</refpurpose>
</refnamediv>
<refsect1>
<title>Popis</title>
<methodsynopsis>
<type>bool</type><methodname>function_exists</methodname>
<methodparam><type>string</type><parameter>function_name</parameter></methodparam>
</methodsynopsis>
<para>
Ov ptomnost funkce <parameter>function_name</parameter> v seznamu
definovanch funkc. Pokud dan funkce existuje, vrac &true;,
jinak &false;.
<informalexample>
<programlisting role="php">
if (function_exists('imap_open')) {
echo "IMAP functions are available.<br>\n";
} else {
echo "IMAP functions are not available.<br>\n";
}
</programlisting>
</informalexample>
Pozn.: nzev funkce me existovat i v ppad, e je samotn funkce
nepouiteln kvli konfiguraci nebo volbm zadanm pi kompilaci.
</para>
<para>
Viz tak <function>method_exists</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.register-shutdown-function">
<refnamediv>
<refname>register_shutdown_function</refname>
<refpurpose>Zaregistrovat funkci pro proveden pi ukonen bhu</refpurpose>
</refnamediv>
<refsect1>
<title>Popis</title>
<methodsynopsis>
<type>int</type><methodname>register_shutdown_function</methodname>
<methodparam><type>string</type><parameter>func</parameter></methodparam>
</methodsynopsis>
<simpara>
Zaregistruje funkci udanou v <parameter>func</parameter> pro proveden
po dokonen bhu skriptu.
</simpara>
<para>
Bn problmy:
</para>
<simpara>
Jeliko tato funkce neme poslat browseru dn vstup, nebudete ji moci
debugovat pomoc pkaz jako <function>print</function> nebo
<function>echo</function>.
</simpara>
</refsect1>
</refentry>
</reference>
<!-- 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:"../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
-->
|