我正在使用一些现有代码,特别是 JQuery 文件上传插件。有一个大类,其中有一些我正在尝试自定义的功能。问题是有几行代码对我来说毫无意义。
protected function get_file_object($file_name) {
//whole bunch of code is here that generates an object file file size
//and other information related to the image that was in the array.
//removed the code to be concise, just know it returns an object.
return $file;
}
protected function get_file_objects() {
return array_values(
array_filter(
array_map(
array($this, 'get_file_object'),
scandir($this->options['upload_dir'])
)));
}
好吧,所以我不明白 array_map 内部发生了什么。我知道数组映射需要一个回调,然后一个数组作为参数。 scandir 从目录中获取数组。
它的回调对我来说毫无意义。我查看了 php 文档中 array() 函数的语法,它没有提到像这样接受两个参数。显然第二个是一个函数,用引号引起来?我理解代码在做什么,但不知道它是如何做的。
这是一些未记录的功能吗?
最佳答案
array_map
的第一个参数是 callable可调用的东西之一是一个数组,其中第一个元素表示实例(如果方法是静态的,则表示类名),第二个元素表示方法名。因此 array($this, 'get_file_object')
引用当前实例的 get_file_object
($this
是当前实例)。
关于PHP 类中数组函数的困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17193287/