<?php
// 测试类,下面举例参见此函数
class Test(){
public function test(){
return 'test';
}
protect function protectTest(){
return 'protect test';
}
private function protectTest(){
return 'private test';
}
}
一、匿名函数
1.基本概念如下:
Closure,匿名函数,又称为Anonymous function(没有定义名字的函数),简单来说就是直接将未命名的函数赋给变量,可以直接用变量来调用函数。
2.使用方法:
可以当作参数传入函数,也可以当作返回值,也可以将匿名函数直接当作参数传递
3.连接匿名函数和外界变量的关键字:USE
闭包可以保存所在代码块上下文的一些变量和值。但是在php默认的情况下,匿名函数不能调用所在代码块的上下文变量,需要通过use关键字。
eg:
function getUser(){
$name = 123;
$f = function() use ($name){
echo $name
}
}
执行$f会输出123
【注意:$name只是引用变量的clone,此时在匿名函数中无法改变变量的值,完全引用需要在变量前加上&,例如use (&$name),此时use里面的参数内存地址与上文相同】
二、method_exits()
1.作用:查找某个函数是否存在于某个类
2.参数:mixed/string $object,string $method_name
3.返回值:boolean
3.eg:method_exits($this,'test')
三、is_callable()
1.作用:查找某个函数是否在某个类中且可以被调用
2.参数:array(mixed/string $object,string $method)
3.返回值:boolean
4.eg:is_callable(array('test','test'))
5.与methos_exits比较查看推荐文章
四、call_user_fun_array()与call_user_fun()
1.作用:调用回调函数,并将参数传入回调函数
2.参数:string $callback,array $para/string $para1,string $para2...
3.返回值:以回调函数为准
4.eg:
call_user_fun_array('test',array('1','2'));
call_user_fun_array(array('Test‘,’test'),array('1','2'));
call_user_fun('test','1','2');
call_user_fun(array('Test‘,’test'),'1','2');
五、