PHP/Funkcijos: Skirtumas tarp puslapio versijų

Ištrintas turinys Pridėtas turinys
S Straipsnis 'PHP Programavimas:Funkcijos' pervadintas į 'PHP:Funkcijos'
131 eilutė:
?>
<!-- Dar daugokai liko :) -->
==Gyvo laiko funkciju naudojimas==
==Runtime function usage==
Programuotojas gali kurti funkcijas vietoj '''function vardas($parametras...) {}''' sintakses. Sitokie metodai leidzia kurti funkcijas dinamiskai.
A developer can create functions inside a PHP script without having to use the '''function name($param...) {}''' syntax. This can done by way of programming that can let you run functions dynamically.
 
===Kvieciant funkcija kurios vardas yra kintamojo reiksme===
===Executing a function that is based on a variable's name===
ThereGalima aretai twodaryti waysdviem to do itbudais, eithertiesiogiu usingkvietimu directarba call or[http://php.net/call_user_func call_user_func] or [http://php.net/call_user_func_array call_user_func_array]:
====DirectTiesioginis functionfunkcijos callkvietimas====
Direct call can be done by simply writing this:
<?php
$texttekstas = "\n\n";
$function_namefunckcijos_pavadinimas = "nl2br";
$outputisvedimas = $function_namefunckcijos_pavadinimas($texttekstas);
echo $outputisvedimas;
?>
Klases kvietimas:
What this simply does is that the name of function is stored in the $function_name variable and then the function named $function_name is executed with $text parameter. PHP evaluates '''$output = $function_name($text);''' to '''$output = nl2br($text);'''.
 
Calling a class
<?php
$to_checkpatikrinti = "bZz...";
$class_nameklases_vardas = "my_classmano_klase";
$function_namefunkcijos_vardas = "my_functionmano_funkcija";
${$class_nameklases_vardas}->$function_namefunkcijos_vardas($to_checkpatikrinti);
?>
Sitas kodas dirba taip pat kaip
Note the use of ${$class_name}. If this was $class_name only, the class it would have been trying to refer would be called $class_name. Instead, we use { and } brackets that are evaluated as $my_class from ${$class_name} .
This statement will be evaluated to
<?php
$to_checkpatikrinti = "bZz...";
$mano_klase->mano_funkcija($patikrinti);
$my_class->my_function($to_check);
?>
====Using call_user_func* functions to call functionsnaudojimas====
Siuose pavyzdziuose, bus naudojama daugiau nei vienas parametras tik parodymui kaip galima naudotis siomis galimybemis:
call_user_func and call_user_func_array only differ that the call_user_func_array alows you to use the second parameter as array, to pass the data very easily and call_user_func has got an infinite number of parameters that is not very useful in a professional way.
In these examples, a class will be used for a wider range of using the example:
<?php
class Some_Class {
function my_function($text1, $text2, $text3) {
$return = $text1."\n\n".$text2."\n\n".$text3;
return $return;
eilutė 171 ⟶ 166:
$my_class=new Some_Class();
?>
Using call_user_func:
<?php
$one = "One";
eilutė 180 ⟶ 175:
echo $result;
?>
Using call_user_func_array:
<?php
$one = "One";
eilutė 189 ⟶ 184:
echo $result;
?>
Kvieciant paprasta funkcija:
 
Using call_user_func:
Note how call_user_func and call_user_func_array are used in both of the examples. call_user_func_array allows the script to execute the function more dynamically.
 
As there was no example of using both of these functions for a non-class function, here they are:
Using call_user_func:
<?php
$one = "One";
eilutė 202 ⟶ 194:
echo $result;
?>
Using call_user_func_array:
<?php
$one = "One";
eilutė 211 ⟶ 203:
echo $result;
?>
====MoreSudetingesni complicated examplespavyzdziai====
<?php
$my_func($param1, $param2);
eilutė 224 ⟶ 216:
call_user_func_array(array(&${$my_class_name}, $my_func), array($param1, $param2));
?>
===CreatingKuriant runtimegyvo functionslaiko funkcijas===
Creating runtime functions is a very good way of making the script more dynamic:
<?php
$function_name=create_function('$one, $two','return $one+$two;');
eilutė 231 ⟶ 222:
echo $function_name("1.5", "2");
?>
[http://php.net/create_function create_function]
create_function creates a function with parameters $one and $two, with a code to evaluate return...
 
When create_function is executed, it stores the function's info in the memory and returns the function's name. This means that you cannot customise the name of the function although that would be preferred by most developers.
[[Category:PHP]]