0

I am just getting into OOP and I am trying to do everything as correct as possible and in the right way.

I have a Product class with 2 methods RetrieveFromDatabase and Display RetrieveFromDatabase access one argument which the product ID and it basically runs a PDO statements which I wrote in my abstract class(dont know if this is the right as PDO already have some of this functions already). It using this ID to fetch the product details.

Display the idea of this function is to render the product details. In this example I only have 1 html but I was planning to add a lot more as I do not know how to pass each of them to a div, which is not in function(normally i do(<div> <?php echo $name; ?> </div>) before I heard about OOP.

Class Product { public $name; public $price; public $length; public $description; public $type; private $results; private $database_connection private function __construct(Database $database_connection) { // database connection $this->database = dbconnect(); } public function RetrieveFromDatabase($id) { $sql->database_connection->query('select Name, price, length, description FROM product WHERE id = :id'); $sql->database_connection->bind(':id', $id); $sql->database_connection->execute(); return $this->results !== false; //Retrieve the product details from database. } public function Display() { //display the product information while(($row = mysql_fetch_row($this->results))) print_r($row); echo <p> $this->database_connection->price </p>; } $Product = New Product(); } 

My Question

How can I write the above code in the proper way without having to include any html in the class file? And also please correct me if I have made any stupid mistakes.

13
  • 1
    Asking to correct stupid mistakes is just wasting the great power of Stack Overflow - isn't it? At the very least you can make DB api usage consistent yourself. Commented Feb 27, 2014 at 15:03
  • And what exactly is this "great power of Stack Overflow" you are talking about if not teaching people how not to make "stupid mistakes"? Commented Feb 27, 2014 at 15:11
  • @holodoc because it's duty of their parents and elementary school teachers. Commented Feb 27, 2014 at 15:13
  • @YourCommonSense I know quiet a few clever guys who find it hard to underunder and use OOP PHP. Its not like I am not try to understand it or not having a go at it. The remember why I want to learn to do it right is because once you start programming the wrong way it hard to change it to the right way and I am sure you know what. You didn't start writing code the right way did you? Commented Feb 27, 2014 at 15:23
  • @YourCommonSense unfortunately I parents are not code writes so didn't have anyone to correct my mistakes only by asking stupid questions when I try I was doing something wrong have I learn this much Commented Feb 27, 2014 at 15:25

1 Answer 1

3

Refrain from trying OOP for a while.

In fact, you made everything as incorrect as possible and in the wrong way.

Not to mention you still have quite vague idea on the API you are using. So, better stick to old good procedural style, until you grasp PHP basics and learn some OOP from various sources.

Update. Do not try to create classes of your own but learn from ready-made ones. Take yourself a framework, which all the learned developers already inclined to, and which will teach you proper OOP approaches by example. Yii and Laravel looks most promising ones.

Sign up to request clarification or add additional context in comments.

4 Comments

-1 I almost never use downvotes. A clear exception for me in this case for the attitude and a "stupid" answer.
I respect your opinion but to me this is the mose helpful answer that can be given. To learn something one have to be at some level, have to have some knowledge to grasp the new one. Without it it will be just a waste of time
Thanks. "various sources" will be good to refer me a good source please. Because a lot of sources are quiet not as good as you think.
@user3057514 I didn't mean it as a some sort of one-time cumulative learning. But rather contrary, I meant you will struck with OOP here and there, and by the time you will get some understanding.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.