我正在使用一些现有的代码,特别是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
的第一个参数是一个可调用对象,其中一个可调用对象是一个数组,其中第一个元素表示实例(如果方法是静态的,则是classname),第二个元素表示方法名。因此,array($this, 'get_file_object')
是指当前实例的get_file_object
($this
是当前实例)。