Jump to content

Regarding PDO's fetch(PDO::FETCH_CLASS) method...


Hall of Famer

Recommended Posts

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.