I'm wondering if an object belonging to a collection class, whilst being iterated on can know it's being iterated and know about the collection class it belongs to? e.g.
<?php
class ExampleObject
{
public function myMethod()
{
if( functionForIterationCheck() ) {
throw new Exception('Please do not call myMethod during iteration in ' . functionToGetIteratorClass());
}
}
}
$collection = new CollectionClass([
new ExampleObject,
new ExampleObject,
new ExampleObject
]);
foreach($collection as $item) {
$item->myMethod(); //Exception should be thrown.
}
(new ExampleObject)->myMethod(); //No Exception thrown.
I've done some Google'ing and couldn't find anything, I'm guessing it's not possible because it breaks an OOP principal somewhere but thought I'd ask anyway!