Hall of Famer Posted March 21, 2012 Share Posted March 21, 2012 Well lets say I have a class called Private_Item with a property like $owner. The class itself and constructor will look like the way below: (Note the Private_Item class extends from a parent class called Item, this is not to be worried here) class Private_Item extends Item{ private $iid; // The itemname and category properties already exist for parent class private $owner; private $quantity; private $status = "Available"; public function __construct($name, $owner, $quantity){ parent::__construct($name); $this->owner = new User($owner); $this->quantity = $quantity; } } Whenever I define a variable like $item = new Private_Item($itemname, $itemowner, $quantity), the $itemowner is used as reference to instantiate a User object, and then passed to the property $this->owner. In this way, I have actually instantiated two objects at once. One for the item, the other for the user who owns this item. I have a question about using PDO's fetch class method though. I'd like to use it to retrieve database info and instantiate a Private_Item object immediately after fetching it from MySQL database. The code looks like below: $item = $stmt->fetch(PDO::FETCH_CLASS, "Private_Item"); The real question is, can the user object still be instantiated in the meantime? I am not quite sure about this, and I may end up having only one $item object but its property $item->owner is not. For those of you familiar with advanced PDO stuff, please lemme know if I can instantiate a subsequent object with the method fetch(PDO::FETCH_CLASS). Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/259441-regarding-pdos-fetchpdofetch_class-method/ 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.