如何向 PHP 中的比较函数发送参数?
首先,我完全使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果它在类外部并且无法访问
$this->params
,那么为什么不将其放在类内部:If it's outside the class and can't access
$this->params
, then why not just put it inside the class:您可以使用 lambda 构造来使用回调中所需的任何变量。 而不是对以下代码中的值
1.5
进行硬编码例如,您可以将其作为变量传递给 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:You can pass it as a variable to a lambda construct:
If you wish to modify
$data
, useuse(&$data)
.