Re: Re: Almost Recursion

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On January 16, 2009 14:45:13 Nathan Rixham wrote:
> Nathan Rixham wrote:
> > Here's a little recursion problem I'd like some input on - the task, to
> > remove recursion from this little set of 3 classes, without removing any
> > functionality.
>
> to be very specific; the problem is caused by a serializer (which cannot
> change); now this serializer calls the get methods automatically, that
> is where it's running in to the recursion; the serializer knows what get
> methods to call by the class type in the setter. (a work around for php
> not supporting strong type setting on properties/attributes)

Don't have time to work out a full solution but I'd try starting by storing 
the town's Id in the TownInformation class instead of the reference to the 
Town object.  Then implement a TownPool that gets populated when a Town's id 
is set.

class TownPool {
    private static $_pool = array();

    public static addTown($id, Town $town) {
        if(self::$_pool[$id] !== null) {
             // Do something here, throw an exception maybe
        }
        self::$_pool[$id] = $town;
    }

    public static getTown($id) {
         return self::$_pool[$id];
    }
}

class Town {
     // .....
     public function setId($id) {
          $this->id = $id;
          TownPool::addTown($id, $this);
     }
}

class TownInformation {
     // Instead of $town;
     private $townId;

     public function setTown(Town $town) {
         $this->townId = $town->getId();
     }

     public function getTown() {
          return TownPool::getTown($this->townId);
     }
}

As an added benefit the TownPool could also make sure that no two Town objects 
have the same id.

You will have to make sure that a Town actually has an id in the 
TownInformation class before trying to add it to the pool.


-- 
Philip Graham
Lightbox Technologies
www.lightbox.ca

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux