PHP函数库分类二十七.docx
《PHP函数库分类二十七.docx》由会员分享,可在线阅读,更多相关《PHP函数库分类二十七.docx(16页珍藏版)》请在冰点文库上搜索。
PHP函数库分类二十七
PHP函数库分类二十七
9.FunctionHanding函数列表1
·call_user_func -Callauserfunctiongivenbythefirstparameter
call_user_func
(PHP4,PHP5)
call_user_func — Callauserfunctiongivenbythefirstparameter
说明
mixed call_user_func ( callback $function [, mixed $parameter [, mixed $... ]])
Callauserdefinedfunctiongivenbythe function parameter.
参数
function
Thefunctiontobecalled.Classmethodsmayalsobeinvokedstaticallyusingthisfunctionbypassing array($classname,$methodname) tothisparameter.Additionallyclassmethodsofanobjectinstancemaybecalledbypassingarray($objectinstance,$methodname) tothisparameter.
parameter
Zeroormoreparameterstobepassedtothefunction.
Note:
Notethattheparametersfor call_user_func() arenotpassedbyreference.
Example#1 call_user_func() exampleandreferences
php
error_reporting(E_ALL);
function increment(&$var)
{
$var++;
}
$a = 0;
call_user_func('increment', $a);
echo $a."";
call_user_func_array('increment', array(&$a)); // You can use this instead before PHP 5.3
echo $a."";
?
>
以上例程会输出:
0
1
返回值
Returnsthefunctionresult,or FALSE onerror.
更新日志
版本
说明
5.3.0
Theinterpretationofobjectorientedkeywordslike parent and self haschanged.Previously,callingthemusingthedoublecolonsyntaxwouldemitan E_STRICT warningbecausetheywereinterpretedasstatic.
范例
Example#2 call_user_func() example
php
function barber($type)
{
echo "You wanted a $type haircut, no problem";
}
call_user_func('barber', "mushroom");
call_user_func('barber', "shave");
?
>
以上例程会输出:
Youwantedamushroomhaircut,noproblem
Youwantedashavehaircut,noproblem
Example#3 call_user_func() usingnamespacename
php
namespace Foobar;
class Foo {
static public function test() {
print "Hello world!
";
}
}
call_user_func(__NAMESPACE__ .'Foo:
:
test'); // As of PHP 5.3.0
call_user_func(array(__NAMESPACE__ .'Foo', 'test')); // As of PHP 5.3.0
?
>
以上例程会输出:
Helloworld!
Helloworld!
Example#4Usingaclassmethodwith call_user_func()
php
class myclass {
static function say_hello()
{
echo "Hello!
";
}
}
$classname = "myclass";
call_user_func(array($classname, 'say_hello'));
call_user_func($classname .':
:
say_hello'); // As of 5.2.3
$myobject = new myclass();
call_user_func(array($myobject, 'say_hello'));
?
>
以上例程会输出:
Hello!
Hello!
Hello!
Example#5Usinglambdafunctionwith call_user_func()
php
call_user_func(function($arg) { print "[$arg]"; }, 'test'); /* As of PHP 5.3.0 */
?
>
以上例程会输出:
[test]
注释
Note:
在函数中注册有多个回调内容时(如使用 call_user_func() 与call_user_func_array()),如在前一个回调中有未捕获的异常,其后的将不再被调用。
·call_user_func_array -Callauserfunctiongivenwithanarrayofparameters
call_user_func_array
(PHP4>=4.0.4,PHP5)
call_user_func_array — Callauserfunctiongivenwithanarrayofparameters
说明
mixed call_user_func_array ( callback $function , array $param_arr )
Callauserdefined function withtheparametersin param_arr.
参数
function
Thefunctiontobecalled.
param_arr
Theparameterstobepassedtothefunction,asanindexedarray.
返回值
Returnsthefunctionresult,or FALSE onerror.
更新日志
版本
说明
5.3.0
Theinterpretationofobjectorientedkeywordslike parent and selfhaschanged.Previously,callingthemusingthedoublecolonsyntaxwouldemitan E_STRICT warningbecausetheywereinterpretedasstatic.
范例
Example#1 call_user_func_array() example
php
function foobar($arg, $arg2) {
echo __FUNCTION__, " got $arg and $arg2 ";
}
class foo {
function bar($arg, $arg2) {
echo __METHOD__, " got $arg and $arg2 ";
}
}
// Call the foobar() function with 2 arguments
call_user_func_array("foobar", array("one", "two"));
// Call the $foo->bar() method with 2 arguments
$foo = new foo;
call_user_func_array(array($foo, "bar"), array("three", "four"));
?
>
以上例程的输出类似于:
foobargotoneandtwo
foo:
:
bargotthreeandfour
Example#2 call_user_func_array() usingnamespacename
php
namespace Foobar;
class Foo {
static public function test($name) {
print "Hello {$name}!
";
}
}
// As of PHP 5.3.0
call_user_func_array(__NAMESPACE__ .'Foo:
:
test', array('Hannes'));
// As of PHP 5.3.0
call_user_func_array(array(__NAMESPACE__ .'Foo', 'test'), array('Philip'));
?
>
以上例程的输出类似于:
HelloHannes!
HelloPhilip!
Example#3Usinglambdafunction
php
$func = function($arg1, $arg2) {
return $arg1 * $arg2;
};
var_dump(call_user_func_array($func, array(2, 4))); /* As of PHP 5.3.0 */
?
>
以上例程会输出:
int(8)
注释
Note:
Referencedvariablesin param_arr arepassedtothefunctionbyreference,regardlessofwhetherthefunctionexpectstherespectiveparametertobepassedbyreference.Thisformofcall-timepassbyreferencedoesnotemitadeprecationnotice,butitisnonethelessdeprecated,andwillmostlikelyberemovedinthenextversionofPHP.Furthermore,thisdoesnotapplytointernalfunctions,forwhichthefunctionsignatureishonored.Passingbyvaluewhenthefunctionexpectsaparameterbyreferenceresultsinawarningandhaving call_user_func() return FALSE (doesnotapplyifthepassedvaluehasareferencecount=1).
Note:
在函数中注册有多个回调内容时(如使用 call_user_func() 与call_user_func_array()),如在前一个回调中有未捕获的异常,其后的将不再被调用。
·create_function -Createananonymous(lambda-style)function
create_function
(PHP4>=4.0.1,PHP5)
create_function — Createananonymous(lambda-style)function
说明
string create_function ( string $args , string $code )
Createsananonymousfunctionfromtheparameterspassed,andreturnsauniquenameforit.
参数
Usuallytheseparameterswillbepassedassinglequotedelimitedstrings.Thereasonforusingsinglequotedstrings,istoprotectthevariablenamesfromparsing,otherwise,ifyouusedoublequotestherewillbeaneedtoescapethevariablenames,e.g. $avar.
args
Thefunctionarguments.
code
Thefunctioncode.
返回值
Returnsauniquefunctionnameasastring,or FALSE onerror.
范例
Example#1Creatingananonymousfunctionwith create_function()
Youcanusethisfunction,to(forexample)createafunctionfrominformationgatheredatruntime:
php
$newfunc = create_function('$a,$b', 'return "ln($a) + ln($b) = " . log($a * $b);');
echo "New anonymous function:
$newfunc ";
echo $newfunc(2, M_E) . "";
// outputs
// New anonymous function:
lambda_1
// ln
(2) + ln(2.718281828459) = 1.6931471805599
?
>
Or,perhapstohavegeneralhandlerfunctionthatcanapplyasetofoperationstoalistofparameters:
Example#2Makingageneralprocessingfunctionwith create_function()
php
function process($var1, $var2, $farr)
{
foreach ($farr as $f) {
echo $f($var1, $var2) . "";
}
}
// 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 "Using the first array of anonymous functions";
echo "parameters:
2.3445, M_PI";
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"** 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 "Using the second array of anonymous functions";
process("Twas brilling and the slithy toves", "Twas the night", $garr);
?
>
以上例程会输出:
Usingthefirstarrayofanonymousfunctions
parameters:
2.3445,M_PI
sometrig:
-1.6291725057799
ahypotenuse:
3.9199852871011
b*a^2=4.8103313314525
min(b^2+a,a^2,b)=8.6382729035898
ln(a)/b=0.27122299212594
Usingthesecondarrayofanonymousfunctions
**"Twasthenight"and"Twasbrillingandtheslithytoves"
**Lookthesametome!
(lookingatthefirst3chars)
CRCs:
-725381282,342550513
similar(a,b)=11(45.833333333333%)
Butperhapsthemostcommonuseforoflambda-style(anonymous)functionsistocreatecallbackfunctions,forexamplewhenusing array_walk() or usort()
Example#3Usinganonymousfunctionsascallbackfunctions
php
$av = array("the ", "a ", "that ", "this ");
array_walk($av, create_function('&$v,$k', '$v = $v . "mango";'));
print_r($av);
?
>
以上例程会输出:
Array
(
[0]=>themango
[1]=>amango
[2]=>thatmango
[3]=>thismango
)
anarrayofstringsorderedfromshortertolonger
php
$sv = array("small", "larger", "a big string", "it is a string thing");
print_r($sv);
?
>
以上例程会输出:
Array
(
[0]=>small
[1]=>larger
[2]=>abigstring
[3]=>itisastringthing
)
sortitfromlongertoshorter
php
usort($sv, create_function('$a,$b','return strlen($b) - strlen($a);'));
print_r($sv);
?
>
以上例程会输出:
Array
(
[0]=>itisastringthing
[1]=>abigstring
[2]=>larger
[3]=>small
)
·forward_static_call -Callastaticmethod
forward_static_call
(PHP5>=5.3.0)
forward_static_call — Callastaticmethod
说明
mixed forward_static_call ( callback $function [, mixed $parameter [, mixed$... ]])
Callsauserdefinedfunctionormethodgivenbythe function parameter,withthefollowingarguments.Thisfunctionmustbecalledwithinamethodcontext,itcan'tbeusedoutsideaclass.Itusesthe latestaticbinding.
参数