mneret Posted March 10, 2008 Share Posted March 10, 2008 Hi, I have a problem with copying objects in PHP 5. In a catalog search results of two different types need to be displayed: class Item_catHosting_catSearch extends Item_catHosting { // member variables protected $displayItem = null; protected $displayArgDao = null; ... // Checks the parameter chosting.pricecutout. If it is not null, call // Item_catHostingCutout::loadItem, otherwise Item_catHosting::loadItem. public function loadItem($argDao) { if (!$argDao->fetch()) return false; printf("parent id %s\n<br />", $argDao->id); // Item is a pricecutout. Therefore create a Item_catHostingCutout // and use its methods to load the record if ( !empty($argDao->pricecutout) ) { $this->displayItem = new Item_catHostingCutout; $this->displayItem->loadItem( $this->displayArgDao ); // Item_catHostingCutout::loadItem contains this statement: // printf("Child Cutout id %s, pricecutout\n<br />", $argDao->id, $argDao->pricecutout); } else { $this->displayItem = new Item_catHosting; $this->displayItem->loadItem( $this->displayArgDao ); // Item_catHosting::loadItem contains this statement: // printf("child catHosting id %s\n<br />", $argDao->id); } return true; } // displays all or several entities of this level // groupParam is a DAO result public static function displayGroup($argDao = null) { $item = new self; $item->prevItem = null; // Copy result object. Iterate through copy in self::loadItem to look for // price cutout hostings. The original is passed to the parent methods to // display the items $cArgDao = clone $argDao; $item->displayArgDao = $cArgDao; // iterate through all items and display them while ( $item->loadItem($argDao)) { $item->displayItem->displayItem(); $prevItem =& $item; $item =& new self; $item->displayArgDao = $prevItem->displayArgDao; $item->prevItem = $prevItem; } } } The output looks like this: parent id 34 child catHosting id 35 parent id 36 child catHosting id 38 parent id 41 child catHosting id 43 parent id 47 child catHosting id 48 parent id 51 child catHosting id 53 Starting point it the static method Item_catHosting_catSearch::displayGroup($argDao), with a PEAR::DataObjects $argDao as it's parameter. This method makes a copy of $argDao called $cArgDao. Item_catHosting_catSearch::loadItem() iterates through $argDao, and depending on a value calls one of two child classes' methods: Either Item_catHostingCutout::loadItem or Item_catHosting::loadItem. The problem is, that although these child classes get $cArgDao as their input value, they seem to continue to iterate through the original $argDao. As a result, the child classes' loadItem methods process rows different than which were checked in Item_catHosting_catSearch. In fact, the output should look like this: parent id 34 child catHosting id 34 parent id 35 child catHosting id 35 parent id 36 child catHosting id 36 parent id 38 child catHosting id 48 .... Anyone an idea, what might be the reason for this behaviour? I have thought about it for some hours now, and can't get the mistake. Thanks in advance, mteren Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.