English 中文(简体)
如何提及后备功能供阵列使用?
原标题:How to pass a reference to a callback function for use in array_filter with extra arguments?

我方法的签名就是这样:

public function ProgramRuleFilter(&$program, $today=null) {

当我援引这一点时,

$programs = array_filter($programs, array($this, ProgramRuleFilter ));

一切照旧。 <>Program RulesFilter方法更新了<编码>$program>/code>阵列,然后如果其成功正确过滤了$programs,则回归真实/数字。

However, now I want to pass an extra argument to the filter, $today. How can I do that?

我试图这样援引:

$programs = array_filter($programs, new CallbackArgs(array($this, ProgramRuleFilter ),$today));

利用这个小阶层作为包裹:

class CallbackArgs {
    private $callback;
    private $args;

    function __construct() {
        $args = func_get_args();
        $this->callback = array_shift($args);
        $this->args = $args;
    }

    function __invoke(&$arg) {
        return call_user_func_array($this->callback, array_merge(array($arg),$this->args));
    }
}

但是,这些方案还没有更新,因此,在一行中,它失去了对原始物体的提及。 我不敢确定如何解决这一问题。

问题回答

第二个论点是array_filter 必须是背书;这意味着<条码>。 自己将称为你的过滤功能。 没有任何办法将<代码>array_filter <>code>称作这一功能,因此,你需要找到某种途径,使<代码>(每日<><>>>值/代码>以某种其他方式纳入你的职能。

这是何时使用封闭式的完美例子,这将使你将某些数据(在这种情况下,<代码>$)的数值约束为功能/备份。 假设你使用购买力平价5.3或以下:

// Assuming $today has already been set

$object = $this; // PHP 5.4 eliminates the need for this
$programs = array_filter( $programs, function( $x ) use ( $today, $object ){
    return $object->ProgramRuleFilter( $x, $today );
});

这从母体范围界定了一条封闭线,使用<条码> 每天<>美元/代码>和<条码> $object,然后将现有的职能称作<条码>Program RulesFilter。 ($object = this>) 取决于以下事实:否则,封锁将无法在你的标本上采用某种方法。 但是,在PHP 5.4中,你可以将<条码>(目标)替换为<条码>。

现在,这在某种程度上是不可取的,因为所有这种关闭都把工作交给了。 更好的办法是利用关闭而不是功能。 因此:

// Assuming $today has already been set

$filter = function( $x ) use ( $today ){
    // Cut and paste the contents of ProgramRuleFilter() here,
    // and make it operate on $x and $today
};
$programs = array_filter( $programs, $filter );

哪一种变化对你来说是最好的,将取决于你们的其余部分的执行情况。 亲爱!

我写了一种新的处理方法:

public static function array_filter_args($array, $callback) {
    $args = array_slice(func_get_args(),2);
    foreach($array as $key=>&$value) {
        if(!call_user_func_array($callback, array_merge(array(&$value),$args))) {
            unset($array[$key]);
        }
    }
    return $array;
}

要求这样做:

$programs = ArrayHelper::array_filter_args($programs, array($this, ProgramRuleFilter ), $today);

我不知道你可以做以下工作:array(&$ Value),但我认为我是这样尝试的,它喜欢这样做。 I m guessing that array_joint is the culprit that defers the changing otherwise.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签