Re: Re: Almost Recursion

From: Date: Fri, 16 Jan 2009 21:50:18 +0000
Subject: Re: Re: Almost Recursion
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-286531@lists.php.net to get a copy of this message
Philip Graham wrote:
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.
thanks for the suggestion, didn't fix but it was a nice idea - managed to figure it out - in short simply made a class Information which TownInformation extends, changed TownInformationCollection to InformationCollection - then all setters tool in Information or InformationCollection none of which had getTown method - so the instances now have the recursion (which is needed) but the class structure does not so the serializer works :) *joy* <?php interface Identifiable { public function getId(); public function setId($id); } interface Informative { public function getInformation(); public function setInformation($information); } abstract class Identifier implements Identifiable { private $id; public function getId() {
    return $this->id;
} public function setId($id) {
    $this->id = $id;
} } class Information extends Identifier implements Informative { private $information; public function getInformation() {
    return $this->information;
} public function setInformation($information) {
    return $this->information = $information;
} } class Town extends Identifier { private $townInformationCollection; public function getTownInformationCollection() {
    return $this->townInformationCollection;
} public function setTownInformationCollection( InformationCollection $townInformationCollection) {
    $this->townInformationCollection = $townInformationCollection;
} } class InformationCollection { private $informationArray = array(); public function getInformationArray() {
    return $this->informationArray;
} public function setInformationArray(array $informationArray) {
    $this->informationArray = array();
    foreach($informationArray as $information) {
    	$this->addInformation($information);
    }
} public function addInformation(Information $information) {
    $this->informationArray[] = $information;
} } class TownInformation extends Information { private $town; public function getTown() {
    return $this->town;
} public function setTown(Town $town) {
    $this->town = $town;
} } $town = new Town(); $town->setId(1); $townInformation = new TownInformation(); $townInformation->setId(12); $townInformation->setInformation('some random info'); $townInformation->setTown($town); $townInformationCollection = new InformationCollection(); $townInformationCollection->setInformationArray( array($townInformation) ); $town->setTownInformationCollection($townInformationCollection); print_r( $town ); print_r( $townInformation ); ?>

« previous php.general (#286531) next »