如何向 PHP 中的比较函数发送参数?

发布于 2024-11-15 09:45:02 字数 1005 浏览 1 评论 0原文

首先,我完全使用 Yii 框架在 PHP 中工作,尽管 Yii 最终与这个问题关系不大。

我有一个类,其中有一个名为 $data 的数组。我想过滤掉数组中与我发送给类的参数不匹配的某些元素(我将在下面添加一些语法以便为您提供更好的想法)。因此,我使用 array_filter,它要求其输入之一是比较函数(即为特定元素返回 true 或 false 的函数。任何导致返回“false”的函数都会从数组中删除)。

问题是,因为输入的函数是用引号输入的,所以我看不到在实际类中具有比较函数的方法。但是当函数在类之外时,我无法调用我需要的实例变量。所以我真正需要的是能够以某种方式调用类外部的实例变量,将实例变量作为参数发送给函数,或者以某种方式将比较函数保留在类内。

对此有什么想法吗?我提到的类是 Yii 中的一个小部件。下面是对该小部件的调用(不是那么重要)。相关参数是“params”。

$this->widget('application.widgets.CListViewParam', array(
    'dataProvider'=>$dataProvider,
    'itemView'=>'_view',
    'params'=>array('receiverId'=>Yii::app()->user->userId),
));

小部件中的类内有一个实例变量来保存参数:

public $params = array();

然后是对 array_filter 和比较函数的调用:

$data = array_filter($data, "filterData");

实际的比较函数并不重要,但下面是骨架。请记住,这是在课堂之外。

function filterData($item) {
    // unable to access $this->params inside of this function!
}

To begin, I'm working entirely in PHP using the Yii framework, although Yii ultimately has little to do with this question.

I've got a class, and inside of it I have an array called $data. I want to filter out certain elements of the array that don't match up with the parameters I'm sending to the class (I'll put some syntax below to give you a better idea). I am therefore using array_filter, and it requires one of its inputs to be a comparison function (ie. one that returns true or false for a specific element. Any that cause a 'false' to be returned are removed from the array).

The problem is that because the function entered is entered in quotes, I don't see a way to have the comparison function within the actual class. But when the function is outside of the class, I can't call the instance variable that I need. So what I really need is to be able to call the instance variable outside of the class somehow, to send the instance variable to the function as a parameter, or to somehow keep the comparison function within the class.

Any ideas on this? The class I mentioned is a widget in Yii. Below is the call to that widget (not that important). The relevant parameter is 'params'.

$this->widget('application.widgets.CListViewParam', array(
    'dataProvider'=>$dataProvider,
    'itemView'=>'_view',
    'params'=>array('receiverId'=>Yii::app()->user->userId),
));

There is an instance variable within a class in the widget to save the parameter:

public $params = array();

Then there is a call to array_filter and the comparison function:

$data = array_filter($data, "filterData");

The actual comparison function is not important, but below is the skeleton. Remember that it is outside of the class.

function filterData($item) {
    // unable to access $this->params inside of this function!
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谜兔 2024-11-22 09:45:02

如果它在类外部并且无法访问 $this->params,那么为什么不将其放在类内部:

class MyClass {

    public $params;

    public function widget() {
        // ...
        $filtered = array_filter($array, array($this, 'filterData'));
    }

    private function filterData($item) {
       // $this->params is now accessible
    }

}

If it's outside the class and can't access $this->params, then why not just put it inside the class:

class MyClass {

    public $params;

    public function widget() {
        // ...
        $filtered = array_filter($array, array($this, 'filterData'));
    }

    private function filterData($item) {
       // $this->params is now accessible
    }

}
寄居者 2024-11-22 09:45:02

您可以使用 lambda 构造来使用回调中所需的任何变量。 而不是对以下代码中的值 1.5 进行硬编码

$array = Array(1.0, 2.0, 3.0, 4.0);

function cmp($x) { return $x > 1.5; }
print_r(array_filter($array, cmp));

例如,您可以将其作为变量传递给 lambda 构造,

$array = Array(1.0, 2.0, 3.0, 4.0);

$data = 1.5;
$lambda = function($x) use ($data) { return $x > $data; };
print_r(array_filter($array, $lambda));

:如果您希望修改 $data,请使用 使用(&$data)

You can use a lambda construct to use any variables you need in a callback. For instance, instead of hardcoding the value 1.5 in this code:

$array = Array(1.0, 2.0, 3.0, 4.0);

function cmp($x) { return $x > 1.5; }
print_r(array_filter($array, cmp));

You can pass it as a variable to a lambda construct:

$array = Array(1.0, 2.0, 3.0, 4.0);

$data = 1.5;
$lambda = function($x) use ($data) { return $x > $data; };
print_r(array_filter($array, $lambda));

If you wish to modify $data, use use(&$data).

昔梦 2024-11-22 09:45:02
$params = $this->params;
$data = array_filter($data, function($item) use ($params){

});
$params = $this->params;
$data = array_filter($data, function($item) use ($params){

});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文