一,变量可以直接传递函数

<?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"));

  

  

更多相关文章

  1. PHP - 函数 & 检测函数/类/方法是否存在
  2. 如何在表单操作中执行PHP函数?
  3. 使用jquery / ajax在CodeIgniter控制器内调用函数
  4. php数字操作,高精度函数,保留计算结果小数位
  5. 函数不返回“functions.php”中的值
  6. php中static 静态变量和普通变量的区别
  7. 您是否认为PHP中的错误形式是在类方法中访问超级全局变量?
  8. 在通过AJAX响应发送的页面上执行javascript函数
  9. php 基本的常用字符串函数

随机推荐

  1. Android--第一行代码笔记(2)
  2. Android的Recyclerview的使用
  3. 【Android】HTTP协议的经典讲解
  4. Android实现透明的颜色效果(zz)
  5. 【解疑答惑】—— android:maxLines="1"
  6. Android重要控件概览(中)
  7. Android艺术开发探索学习笔记——第二章:I
  8. 三位一体!Android Ice Cream Sandwich可能
  9. Android进程间通信--消息机制及IPC机制实
  10. Android 高效的SQLite型数据库greenDAO使