Jump to content

get_class_methods doesn't return all methods


NotionCommotion

Recommended Posts

I'm confused.  Why do I not get an error with $it->getSubPathName() and $it->getSubPath()?

$it = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir));
$m = array_flip(get_class_methods($it));
printf('class: %20s getSubPathName: %s getSubPath: %s key: %s'.PHP_EOL, get_class($it), isset($m['getSubPathName'])?'y':'n', isset($m['getSubPath'])?'y':'n', isset($m['key'])?'y':'n');
print_r(get_class_methods($it));
echo 'SubPathName: ' . $it->getSubPathName() . PHP_EOL;
echo 'SubPath:     ' . $it->getSubPath() . PHP_EOL;
echo 'Key:         ' . $it->key() . PHP_EOL.PHP_EOL;

output:

class: RecursiveIteratorIterator getSubPathName: n getSubPath: n key: y
Array
(
    [0] => __construct
    [1] => rewind
    [2] => valid
    [3] => key
    [4] => current
    [5] => next
    [6] => getDepth
    [7] => getSubIterator
    [8] => getInnerIterator
    [9] => beginIteration
    [10] => endIteration
    [11] => callHasChildren
    [12] => callGetChildren
    [13] => beginChildren
    [14] => endChildren
    [15] => nextElement
    [16] => setMaxDepth
    [17] => getMaxDepth
)
SubPathName: .
SubPath:
Key:         src/.


 

Link to comment
Share on other sites

A lot of the iterator classes in PHP do some screwy things. This is apparently one of them: it implements (an internal version of) __call that sends method calls to the inner iterator. So you're getting magic behavior.

Why does it do that? I don't know. It dates back to PHP 4 and I wouldn't be surprised if nobody has thought about it since then.

Link to comment
Share on other sites

1 hour ago, NotionCommotion said:

Also suspect it is documented somewhere but I just looked at the wrong place.  Must say, though, that it is well hidden documentation.

Yea, It's briefly noted in the documentation for the Iterator class.

Quote

Note:

This class permits access to methods of the inner iterator via the __call magic method.

I first noticed this behavior when working with FilterIterator.  Seems kind of strange and dumb to me, but oh well.

  • Like 1
Link to comment
Share on other sites

19 hours ago, kicken said:

Yea, It's briefly noted in the documentation for the Iterator class.

class IteratorIterator implements OuterIterator and this class permits access to methods of the inner iterator via the __call magic method.

class RecursiveIteratorIterator implements OuterIterator 

interface OuterIterator extends Iterator

Should I conclude that any class that implements OuterIterator permits access to methods of the inner iterator via the __call magic method, and not just IteratorIterator?

Thanks

PS.  Totally off topic, but is there a way to embed a link when one quotes something (i.e. your quote would include https://www.php.net/manual/en/class.iteratoriterator.php)?

Array
(
    [0] => RecursiveIteratorIterator
    [1] => IteratorIterator
    [2] => FilterIterator
    [3] => RecursiveFilterIterator
    [4] => CallbackFilterIterator
    [5] => RecursiveCallbackFilterIterator
    [6] => ParentIterator
    [7] => LimitIterator
    [8] => CachingIterator
    [9] => RecursiveCachingIterator
    [10] => NoRewindIterator
    [11] => AppendIterator
    [12] => InfiniteIterator
    [13] => RegexIterator
    [14] => RecursiveRegexIterator
    [15] => RecursiveTreeIterator
)

 

Link to comment
Share on other sites

1 hour ago, NotionCommotion said:

class IteratorIterator implements OuterIterator and this class permits access to methods of the inner iterator via the __call magic method.

class RecursiveIteratorIterator implements OuterIterator 

interface OuterIterator extends Iterator

Should I conclude that any class that implements OuterIterator permits access to methods of the inner iterator via the __call magic method, and not just IteratorIterator?

"Correlation does not imply causation."

Looking through the source, it looks like the ones that support it are:
RecursiveIteratorIterator, IteratorIterator, FilterIterator, RecursiveFilterIterator, CallbackFilterIterator, RecursiveCallbackFilerIterator, ParentIterator, not SeekableIterator, LimitIterator, CachingIterator, RecursiveCachingIterator, NoRewindIterator, AppendIterator, InfiniteIterator, RegexIterator, RecursiveRegexIterator, not EmptyIterator (duh), and not RecursiveTreeIterator.

1 hour ago, NotionCommotion said:

PS.  Totally off topic, but is there a way to embed a link when one quotes something (i.e. your quote would include https://www.php.net/manual/en/class.iteratoriterator.php)?

If there was a link then your quote of kicken's post would have included it...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.