Re: DB_DataObject Can getLink() return child class of DB_DataObject
| From: | Ben Ford | Date: | Thu, 31 Mar 2005 23:25:57 +0000 |
| Subject: | Re: DB_DataObject Can getLink() return child class of DB_DataObject | ||
| References: | 1 2 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-18447@lists.php.net to get a copy of this message | ||
Justin Patrin wrote:
On Thu, 31 Mar 2005 14:55:24 -0800, Ben Ford <ben@naturestears.com> wrote:Make it your own class which extends DB_DO and put someIs there an existing way to have getlink() return a child class of DB_DataObject? I was going to extend the getLink() to allow this but wanted some feedback first. Also, is there a better way to do this? ie. require_once 'DataObjects/Odb_item_line.php'; class ODBItemLine extends DataObjects_Odb_item_line {I would suggest using the factory anyway. getLink uses the factory to get the DO of the linked object. It's best to put code specific to the table into the autogenerated class. You can also always change which object any of your autogenerated clases extends from. Is the way I'm extending each DataObject unusual? In my mind, it has both positive and negative effects. The negative might out way the positive and that is exactly what I am trying to figure out.public function getDescription() { $item = $this->getLink('odbItemId'); //returns wrong type! return $item->toString(); //WILL NOT WORK!! }} require_once 'DataObjects/Odb_item.php'; class ODBItem extends DataObjects_Odb_item {/** @return string String representation of item. * This can change based on various properties. */ public function toString() { //Fancy logic to calculate the name goes here... return $itemName; }} One way around this:Put my extended code directly into the Auto-Generated class. But thisties my class names to closely to the table names. I like the extra degree of space I get when extending each class myself. I would also have to use the factory method of instantiation which for some reason I was trying to avoid... Thanks in advance.
generic code in there. Then you don't have to duplicate things. What does this mean?This is sort of how I was planning to extend getLink() to return a custom class instead of using the factory. My_DB_DataObject extends DB_DataObject { function &getLink($row, $table = null, $link = false,$class=null) //copy and paste original getLink() code ... //STAR MY CHANGES if( isset($class) ) { $obj = new $class(); } else { $obj = $this->factory($table); } //END MY CHANGES ... } } Thank you for the quick response...