Jump to content

Need Help Understanding OO-Programming


Jacky_sd
 Share

Recommended Posts

Hello guys!

 

I really really really hope anyone can help me because I am quite desperate about a little project I'm making.

 

I want to construct a sort of online catalog for books with a "shoppingcart" and a profile for every user, but object orientated.

 

I have a database with the table products which contains: ID, title, author, genre, price etc etc etc

 

My main question is: How can I get one single field from the database with e.g. a function called getPrice();

 

I tried everything but nothing displays the data the way i want to. I simply want to have a sql-statement in this function and when I am calling the function getPrice() later on it displays just the number and no Array, code or whatever.

 

Is there a way to call getPrice() and get "20.00" back?

 

Please Help!

 

 
Link to comment
Share on other sites

Rather than have getPrice() do a query, and then have other methods doing their own queries, just do one query at the very start to get all the information at once. Store that in an array inside the object, then make getPrice() just get the one value out of it.

class Product {

	private $data = array();

	public function __construct($id) {
		$this->data = // get all the data for product #id
	}

	public function getPrice() {
		return $this->data["price"];
	}

}
A bit simplistic but that should demonstrate my point.
  • Like 2
Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

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.

 Share

×
×
  • 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.