Resolve php endless recursion issue

Posted by Matt on Stack Overflow See other posts from Stack Overflow or by Matt
Published on 2010-05-22T11:09:34Z Indexed on 2010/05/22 11:40 UTC
Read the original article Hit count: 291

Filed under:
|
|
|

Hey all,

I'm currently running into an endless recursion situation.

I'm implementing a message service that calls various object methods.. it's quite similar to observer pattern..

Here's whats going on:

Dispatcher.php

class Dispatcher {
...
    public function message($name, $method) {

    // Find the object based on the name
    $object = $this->findObjectByName($name);

    // Slight psuedocode.. for ease of example
    if($this->not_initialized($object))
        $object = new $object(); // This is where it locks up.
    }

    return $object->$method();
...
}

class A {
    function __construct()
    {
        $Dispatcher->message("B", "getName");
    }

    public function getName() {
        return "Class A";
    }

}

class B {
    function __construct()
    {
            // Assume $Dispatcher is the classes
        $Dispatcher->message("A", "getName");
    }

    public function getName() {
        return "Class B";
    }

}

It locks up when neither object is initialized. It just goes back and forth from message each other and no one can be initialized.

I'm looking for some kind of queue implementation that will make messages wait for each other.. One where the return values still get set. I'm looking to have as little boilerplate code in class A and class B as possible

Any help would be immensely helpful..

Thanks! Matt Mueller

© Stack Overflow or respective owner

Related posts about php

Related posts about recursion