这种方法来自何处

时间:2021-03-21 20:19:36

I'm currently working through a php how to book and a little puzzled by the following. Part of The code is;

我目前正在通过PHP如何预订,有点困惑以下。部分代码是;

$filesInFolder = new DirectoryIterator( $folder);
$file = $filesInFolder->current();
$filename = $file->getFilename();

I (think that I) understand that $filesInFolder = new DirectoryIterator( $folder) creates a new instance of DirectoryIterator( $folder) in $filesInFolder and that $file = $filesInFolder->current() is calling the method current() in to $file.

我(我想)我明白$ filesInFolder = new DirectoryIterator($ folder)在$ filesInFolder中创建一个新的DirectoryIterator($ folder)实例,而$ file = $ filesInFolder-> current()正在调用方法current()in到$ file。

What I don't understand is where is the method getFilename() in $file->getFilename() coming from.

我不明白的是$ file-> getFilename()中的方法getFilename()来自哪里。

Is $file = $filesInFolder->current() creating a new instance of the class in $file or is something else happening?

是$ file = $ filesInFolder-> current()在$ file中创建类的新实例还是发生了其他事情?

Thanks

2 个解决方案

#1


1  

Your question is quite valid. It is indeed confusing. What's more, the step of getting ->current() can be omitted. This would also work:

你的问题非常有效。这确实令人困惑。更重要的是,获取 - > current()的步骤可以省略。这也有效:

$filesInFolder = new DirectoryIterator( $folder);
$filename = $filesInFolder->getFilename();

The DirectoryIterator class exposes a method current:

DirectoryIterator类公开一个方法当前:

Return the current DirectoryIterator item.

返回当前的DirectoryIterator项。

This is in fact an implementation of the current method of the Iterator interface.

这实际上是Iterator接口的当前方法的实现。

The confusing thing here, is that the object returned by DirectoryIterator::current is again of the DirectoryIterator class. This class combines into one what you would expect to be segregated in separate classes:

令人困惑的是,DirectoryIterator :: current返回的对象又是DirectoryIterator类。这个类合并为一个你期望在不同的类中隔离的类:

  • the SeekableIterator methods (such as next, rewind, seek), which work on the collection of files, and
  • SeekableIterator方法(例如next,rewind,seek),它们处理文件集合,以及

  • the current-item methods (such as getFilename, getSize), which work on the current file.
  • current-item方法(如getFilename,getSize),它们对当前文件起作用。

So as these come together in one object, you don't need to call current, although some would still prefer that for consistency with how it works with other Iterable classes.

因此,当这些在一个对象中聚集在一起时,您不需要调用current,尽管有些人仍然希望与其他Iterable类的工作方式保持一致。

NB: Usually you would write code on an Iterable this way:

注意:通常你会以这种方式在Iterable上编写代码:

foreach (new DirectoryIterator( $folder) as $file) {
    $filename = $file->getFilename();
    // ... etc, maybe break;
}

This way you also deal with the case where there is no matching file at all.

这样你也可以处理根本没有匹配文件的情况。

#2


0  

  1. Creates an instance of the DirectoryIterator

    创建DirectoryIterator的实例

    $filesInFolder = new DirectoryIterator($folder);
    
  2. Returns the DirectoryIterator object (not a new instance) assigned to $filesInFolder

    返回分配给$ filesInFolder的DirectoryIterator对象(不是新实例)

    $file = $filesInFolder->current();
    
  3. Returns the file name of the DirectoryIterator object assigned to $file.

    返回分配给$ file的DirectoryIterator对象的文件名。

    $filename = $file->getFilename();
    

#1


1  

Your question is quite valid. It is indeed confusing. What's more, the step of getting ->current() can be omitted. This would also work:

你的问题非常有效。这确实令人困惑。更重要的是,获取 - > current()的步骤可以省略。这也有效:

$filesInFolder = new DirectoryIterator( $folder);
$filename = $filesInFolder->getFilename();

The DirectoryIterator class exposes a method current:

DirectoryIterator类公开一个方法当前:

Return the current DirectoryIterator item.

返回当前的DirectoryIterator项。

This is in fact an implementation of the current method of the Iterator interface.

这实际上是Iterator接口的当前方法的实现。

The confusing thing here, is that the object returned by DirectoryIterator::current is again of the DirectoryIterator class. This class combines into one what you would expect to be segregated in separate classes:

令人困惑的是,DirectoryIterator :: current返回的对象又是DirectoryIterator类。这个类合并为一个你期望在不同的类中隔离的类:

  • the SeekableIterator methods (such as next, rewind, seek), which work on the collection of files, and
  • SeekableIterator方法(例如next,rewind,seek),它们处理文件集合,以及

  • the current-item methods (such as getFilename, getSize), which work on the current file.
  • current-item方法(如getFilename,getSize),它们对当前文件起作用。

So as these come together in one object, you don't need to call current, although some would still prefer that for consistency with how it works with other Iterable classes.

因此,当这些在一个对象中聚集在一起时,您不需要调用current,尽管有些人仍然希望与其他Iterable类的工作方式保持一致。

NB: Usually you would write code on an Iterable this way:

注意:通常你会以这种方式在Iterable上编写代码:

foreach (new DirectoryIterator( $folder) as $file) {
    $filename = $file->getFilename();
    // ... etc, maybe break;
}

This way you also deal with the case where there is no matching file at all.

这样你也可以处理根本没有匹配文件的情况。

#2


0  

  1. Creates an instance of the DirectoryIterator

    创建DirectoryIterator的实例

    $filesInFolder = new DirectoryIterator($folder);
    
  2. Returns the DirectoryIterator object (not a new instance) assigned to $filesInFolder

    返回分配给$ filesInFolder的DirectoryIterator对象(不是新实例)

    $file = $filesInFolder->current();
    
  3. Returns the file name of the DirectoryIterator object assigned to $file.

    返回分配给$ file的DirectoryIterator对象的文件名。

    $filename = $file->getFilename();