PHP方法可以检测它是否在循环中被调用? [重复]

Possible Duplicate:
Return a loop in function php

To make my question more clear, I'll explain the situation a bit... I'm trying to make a simple yet powerfull PHP-ORM-tool. First versions ( only 25 kB of code ) and tests are quite promising, it has e.g. lazy-loading. Now I'm optimizing the thing by e.g. minimizing the number of queries, ...

For the lazy-loading, I use a Proxy-class. The Child-property of the Parent-class is a Proxy at first. That Proxy contains an empty object...

class Parent {
    getChild() { ... }
    //other code
}

class Child {
    getName() { ... }
    //other code
}

class Proxy {
    $object = false;    
    public function _query() { /*some code to get the objects*/ }
    __call() { 
        if(!$objects) 
            $this->_query();
        //perform called function on the $object(s)
    }
    //other code
}

When we ask the Child from the Parent, we assume it is a Child, but in fact it is a Proxy. As long as we don't do anything with it, we don't have to query the database... Whenever we ask the Child something ( like getName() ), the magic call function comes in action, queries the database an performs the called function on the new object. The principle is easy, but it is a lot more difficult in code... (it also support lists of objects, triggers in loops, has arraysaccess, the querying is quite complex too, ...)

The problem now is the following:

foreach( $parents as $parent ) {
    echo $parent->getChild()->getName();
}

Every call in the foreach loop, triggers a query to the database... I don't want that! Because I already know that I want the children of a list of parents (thats what the human mind says at least...)

Let's assume I have knowledge of all the Proxies of the same type, I would like to do something like this:

class Proxy {
    _query() {
        ## some code to test if the call originates from within a loop ##
            //if so: fill all the Proxies of this type with their object(s)
            //else fill this Proxy with its object(s)
    }
    //other code
}

I know I'm simplyfying this a bit, but that's the general idea...

debug_backtrace can give me the method from which a function was called, but I want information on the loop-structures... (if possible even the original list etc...)

Why not just using a parameter like

function _query($loop = false)

when called in a loop you could use _query(true), to tell the function you are in a loop.

Otherwise you could use an internal counter-variable to count the calls for the query. ;)

No, it can't detect that as far as I know but you could always send a parameter when calling the method no?

You can't programmatically, but one hack I can think of is to set a global flag variable before any loop?

You can examine the call stack, but the overhead will be huge.