Referencing through a container class?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marcin Dobrucki

    Referencing through a container class?

    I am writing an app where an "environmen t" contains a few managers which
    arbitrate access to objets. The scenario is something like this:

    class Env {
    var $db;
    var $manager_a;
    var $manager_b;

    function Env () {

    // start pseudo code for db connection:
    $this->db = open_connection ();
    // end pseudocode
    $this->manager_a = new A(&this->db);
    $this->manager_b = new B(&this->db);
    }
    }

    class A {
    var $db_ref;
    function A ($ref) {
    $this->db_ref = $ref;
    }
    }

    class B {
    var $db_ref;
    function B ($ref) {
    $this->db_ref = $ref;
    }
    }

    This works, but now, I sometimes need to be able to access one manager
    from within the other, in order to arbitrate access and so on. The way
    I do it now is something like this:

    class B {
    var $db_ref;
    var $manager_a_ref;
    function B ($ref, $ref_A) {
    $this->db_ref = $ref;
    $this->manager_a_re f = $ref_A;
    }
    }

    // and then in Env:
    // $this->manager_b = new B(&$this->db, &$this->manager_a)

    But the app is growing, and more managers are being added, so I am going
    to be running into a problem of having to store a good deal of
    references within each manager. So I was pondering if it'd be possible
    to do something like this:

    function Env () {
    ....
    $this->manager_a = new A(&$this);
    ...
    }

    Allowing me to reference back (within some manager)
    $this->ref->manager_othe r->... get what I want. I've recoded the above
    example to try to do this, but it fails, and I am not sure if I got lost
    in syntax or if this is complete bollocks. Any ideas? Any alternative
    solutions?

    /Marcin
Working...