如何在 array_map 的可调用对象中实现类方法
我正在尝试创建一个类来处理数组,但我似乎无法让 array_map()
在其中工作。
$array = [1,2,3,4,5,6,7,8,9,10];
class test {
public $values;
public function adding($data) {
$this->values = array_map($this->dash(), $data);
}
public function dash($item) {
return '-' . $item . '-';
}
}
var_dump($array);
$test = new test();
$test->adding($array);
// Expected: -1-,-2-,-3-,-4-...
var_dump($test->values);
输出
array(10) { [0]=>; int(1) [1]=> int(2) [2]==> int(3) [3]==> int(4) [4]==> int(5) [5]==> int(6) [6]==> int(7) [7]==> int(8) [8]==> int(9)[9]==> int(10) }
Warning: Missing argument 1 for test::dash(), called in [...]\arraytesting.php on line 11 and defined in [...]\arraytesting.php on line 15
Warning: array_map() expects parameter 1 to be a valid callback, function '--' not found or invalid function name in [...]\arraytesting.php on line 11 NULL
我做错了什么或者这个函数在类中不起作用?
I am trying to create a class to handle arrays but I can't seem to get array_map()
to work in it.
$array = [1,2,3,4,5,6,7,8,9,10];
class test {
public $values;
public function adding($data) {
$this->values = array_map($this->dash(), $data);
}
public function dash($item) {
return '-' . $item . '-';
}
}
var_dump($array);
$test = new test();
$test->adding($array);
// Expected: -1-,-2-,-3-,-4-...
var_dump($test->values);
This outputs
array(10) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) [5]=> int(6) [6]=> int(7) [7]=> int(8) [8]=> int(9) [9]=> int(10) }
Warning: Missing argument 1 for test::dash(), called in [...]\arraytesting.php on line 11 and defined in [...]\arraytesting.php on line 15
Warning: array_map() expects parameter 1 to be a valid callback, function '--' not found or invalid function name in [...]\arraytesting.php on line 11 NULL
What am I doing wrong or does this function just not work inside classes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您以错误的方式指定
dash
作为回调。这是行不通的:
这是:
阅读回调可能采取的不同形式 在这里。
You are specifying
dash
as the callback in the wrong way.This does not work:
This does:
Read about the different forms a callback may take here.
当使用类方法作为
array_map()
和usort()
等函数的回调时,您必须将回调作为二值数组发送。第二个值始终是字符串形式的方法名称。第一个值是上下文(类名或对象)When using a class method as a callback for functions like
array_map()
andusort()
, you have to send the callback as two-value array. The 2nd value is always the name of the method as a string. The 1st value is the context (class name or object)array_map($this->dash(), $data)
使用 0 个参数调用$this->dash()
并使用返回值作为回调函数适用于数组的每个成员。您需要使用 array_map(array($this,'dash'), $data) 来代替。array_map($this->dash(), $data)
calls$this->dash()
with 0 arguments and uses the return value as the callback function to apply to each member of the array. You wantarray_map(array($this,'dash'), $data)
instead.它必须读取
array
-thing 是 对象实例方法的 PHP 回调。对常规函数的回调定义为包含函数名称 ('functionName'
) 的简单字符串,而静态方法调用则定义为array('ClassName, 'methodName')
或像这样的字符串:'ClassName::methodName'
(从 PHP 5.2.3 开始有效)。It must read
The
array
-thing is the PHP callback for a object instance method. Callbacks to regular functions are defined as simple strings containing the function name ('functionName'
), while static method calls are defined asarray('ClassName, 'methodName')
or as a string like that:'ClassName::methodName'
(this works as of PHP 5.2.3).array_map
接受回调 作为它的第一个参数。静态方法的回调是这样写的:
这意味着,在您的具体情况下,您将使用:
有关回调的更多信息,请参阅 PHP 手册的这一部分:本文档中使用的伪类型和变量 - 回调。
array_map
takes a callback as its first parameter.And a callback to a static method is written like this :
Which means that, in your specific case, you'd use :
For more informations about callbacks, see this section of the PHP manual : Pseudo-types and variables used in this documentation - callback.
如果类属于不同的命名空间,则需要使用完整的命名空间类名。下面是使用 CakePHP 实用程序类的示例:
这不会工作:
这会工作:
In case the class belongs to a different namespace, you need to use the complete namespaced class name. Below is an example using a CakePHP Utility class:
This will not work:
This will work:
//常规函数:
array_map('MyFunction', $array);
//类中的静态函数:
array_map(array('MyClass', 'MyFunction'), $array);
//来自对象的函数:
array_map(array($this, 'MyFunction'), $array);
//来自父类的函数
array_map(array($this, 'parent::MyFunction'), $array);
//Regular functions:
array_map('MyFunction', $array);
//static functions in a class:
array_map(array('MyClass', 'MyFunction'), $array);
//functions from an object:
array_map(array($this, 'MyFunction'), $array);
//functions from an parent class
array_map(array($this, 'parent::MyFunction'), $array);