php变量函数,回调函数
16lz
2021-01-22
一,变量可以直接传递函数
<?php
function demo($num , $n )//$n是个函数
{
for($i=0;$i<$num;++$i)
{
if($n($i))
{
echo $i."<br>";
}
}
}
function funcall($i)
{
return $i%2==0;
}
demo(100,"funcall");
二, 使用call_user_func_array
<?php
function demo($num , $n )
{
for($i=0;$i<$num;++$i)
{
if(call_user_func_array($n,array($i)))
{
echo $i."<br>";
}
}
}
function funcall($i)
{
return $i%2==0;
}
demo(100,"funcall");
三,类中函数作为回调函数
(1)类成员函数
<?php
function demo($num , $n )//$n是个函数
{
for($i=0;$i<$num;++$i)
{
if($n($i))
//if(call_user_func_array($n,array($i)))
{
echo $i."<br>";
}
}
}
function funcall($i)
{
return $i%2==0;
}
class Filter
{
function fun($i)
{
return $i%2==0;
}
static function sfun($i)
{
return $i%3 ==0;
}
}
demo(100,array(new Filter(),"fun"));
(2)类静态函数
<?php
function demo($num , $n )//$n是个函数
{
for($i=0;$i<$num;++$i)
{
if($n($i))
//if(call_user_func_array($n,array($i)))
{
echo $i."<br>";
}
}
}
function funcall($i)
{
return $i%2==0;
}
class Filter
{
function fun($i)
{
return $i%2==0;
}
static function sfun($i)
{
return $i%3 ==0;
}
}
demo(100,array("Filter","sfun"));
更多相关文章
- PHP - 函数 & 检测函数/类/方法是否存在
- 如何在表单操作中执行PHP函数?
- 使用jquery / ajax在CodeIgniter控制器内调用函数
- php数字操作,高精度函数,保留计算结果小数位
- 函数不返回“functions.php”中的值
- php中static 静态变量和普通变量的区别
- 您是否认为PHP中的错误形式是在类方法中访问超级全局变量?
- 在通过AJAX响应发送的页面上执行javascript函数
- php 基本的常用字符串函数