NotionCommotion Posted April 30, 2022 Share Posted April 30, 2022 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/. Quote Link to comment https://forums.phpfreaks.com/topic/314745-get_class_methods-doesnt-return-all-methods/ Share on other sites More sharing options...
requinix Posted April 30, 2022 Share Posted April 30, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/314745-get_class_methods-doesnt-return-all-methods/#findComment-1595841 Share on other sites More sharing options...
NotionCommotion Posted May 1, 2022 Author Share Posted May 1, 2022 Thanks requinix, I suspected something like that but could find no documentation. Also suspect it is documented somewhere but I just looked at the wrong place. Must say, though, that it is well hidden documentation. Quote Link to comment https://forums.phpfreaks.com/topic/314745-get_class_methods-doesnt-return-all-methods/#findComment-1595858 Share on other sites More sharing options...
kicken Posted May 1, 2022 Share Posted May 1, 2022 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/314745-get_class_methods-doesnt-return-all-methods/#findComment-1595861 Share on other sites More sharing options...
NotionCommotion Posted May 2, 2022 Author Share Posted May 2, 2022 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 ) Quote Link to comment https://forums.phpfreaks.com/topic/314745-get_class_methods-doesnt-return-all-methods/#findComment-1595875 Share on other sites More sharing options...
requinix Posted May 2, 2022 Share Posted May 2, 2022 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... Quote Link to comment https://forums.phpfreaks.com/topic/314745-get_class_methods-doesnt-return-all-methods/#findComment-1595877 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.