I am writing a small Product List with CRUD functions. I implemented it all in a procedural style, now I am reworking the option on the PLO, and the question in my head how the PLO works is a little bit suspended. I created three classes for each product, each class describes the CRUD method for its product. Made a common parent class Product, added some common variables for all products, and inherited them. Created a ProductInterface, where the method of these products and the parent class Product implements productInterface placed the contracts, respectively, the product classes get these methods, these arrows in the IDE appeared on the side. Tell me how to use OOP further to see it in action.

Parent class Product.php

 require_once 'Interface/ProductInterface.php'; class Product implements productInterface { private $conn; private $table_name = "Books"; public $id; public $sku; public $img; public $name; public $description; public $price; public function __construct($db){ $this->conn = $db; } public function readAll(){ $query = "SELECT * FROM " . $this->table_name . " ORDER BY id DESC"; $stmt = $this->conn->prepare( $query ); $stmt->execute(); return $stmt; } 

Interface ProductInterface.php

 interface productInterface { public function readAll(); public function readOne(); public function create(); public function delete(); } 

And the class product itself is allowed Books

 require_once 'Product.php'; class books extends Product { public $author; public $weight; // object properties public function readAll(){ } //query to read one book and all data of it by `id` from `book` table function readOne(){ $query = "SELECT sku, name, img, author, price, weight, description FROM " . $this->table_name . " WHERE id = ? LIMIT 0,1"; $stmt = $this->conn->prepare( $query ); $stmt->bindParam(1, $this->id); $stmt->execute(); $row = $stmt->fetch(PDO::FETCH_ASSOC); //data that will be executed in `book_id` $this->sku = $row['sku']; $this->name = $row['name']; $this->img = $row['img']; $this->author = $row['author']; $this->description = $row['description']; $this->weight = $row['weight']; $this->price = $row['price']; } // query to create new book function create(){ //query for creating new book in table $query = "INSERT INTO " . $this->table_name . " (sku, name, author, description, price, weight)" . "VALUES ('{$this->sku}','{$this->name}','{$this->author}','{$this->description}','{$this->price}','{$this->weight}');"; $stmt = $this->conn->prepare($query); // posted values $this->sku=htmlspecialchars(strip_tags($this->sku)); $this->name=htmlspecialchars(strip_tags($this->name)); $this->author=htmlspecialchars(strip_tags($this->author)); $this->description=htmlspecialchars(strip_tags($this->description)); $this->price=htmlspecialchars(strip_tags($this->price)); $this->weight=htmlspecialchars(strip_tags($this->weight)); // bind values $stmt->bindParam(":sku", $this->sku); $stmt->bindParam(":name", $this->name); $stmt->bindParam(":author", $this->author); $stmt->bindParam(":description", $this->description); $stmt->bindParam(":price", $this->price); $stmt->bindParam(":weight", $this->weight); if($stmt->execute()){ return true; }else{ return false; } } // query for updating book and change book data in simple form function update(){ $query = "UPDATE " . $this->table_name . " SET sku = :sku, name = :name, author = :author, price = :price, weight = :weight, description = :description WHERE id = :id"; $stmt = $this->conn->prepare($query); // posted values that user can change $this->sku=htmlspecialchars(strip_tags($this->sku)); $this->name=htmlspecialchars(strip_tags($this->name)); $this->author=htmlspecialchars(strip_tags($this->author)); $this->weight=htmlspecialchars(strip_tags($this->weight)); $this->price=htmlspecialchars(strip_tags($this->price)); $this->description=htmlspecialchars(strip_tags($this->description)); $this->id=htmlspecialchars(strip_tags($this->id)); // bind parameters $stmt->bindParam(':sku', $this->sku); $stmt->bindParam(':author', $this->author); $stmt->bindParam(':name', $this->name); $stmt->bindParam(':price', $this->price); $stmt->bindParam(':weight', $this->weight); $stmt->bindParam(':description', $this->description); $stmt->bindParam(':id', $this->id); // execute the query if($stmt->execute()){ return true; } return false; } //query to delete the product by id function delete(){ $query = "DELETE FROM " . $this->table_name . " WHERE id = ?"; $stmt = $this->conn->prepare($query); $stmt->bindParam(1, $this->id); if($result = $stmt->execute()){ return true; }else{ return false; } } } 

Maybe you can somehow inherit these methods so that in each class you do not write them, the readAll and delete methods do not differ at all except the table is different in the database.

Code implementation of the readAll method.

 <?php // read the books from the database $books = new Books($db); $stmt = $books->readAll(); while ($books = $stmt->fetch(PDO::FETCH_ASSOC)) { extract($books); echo "<div data-price='{$price}' class='item'>"; echo "<a href='book_id.php?id={$id}'><img class='product' src='UI/images/{$img}'></a> "; echo "<div class='info'>"; echo "<h6 align='center'> <a href='book_id.php?id={$id}'></a>{$name}</h6> "; echo "<p class='descroption'>ID number: {$id} </p>"; echo "<p class='descroption'>SKU number: {$sku} </p>"; echo "<p class='descroption'>Price: {$price} <i class='glyphicon-euro'></i></p>"; echo "<p class='descroption'>Weight: {$weight} kg</p>"; echo "</div>"; echo "</div>"; } ?> 

Waiting for your constructive criticism and advice.

  • one
    why to carry in final classes? it should be at the Product level as well as the table_name , so in general, the entire readAll method can be implemented at the level of the parent class. Children only have to override the value of this variable. Processing incoming data should also be in another place. and more about variable variables read. a bunch of lines can be abbreviated to a simple loop with the names - teran
  • one
    read more about template engines: smarty, twig, etc. And fetch do in the methods themselves, why do this in the calling code? and generally it will be more convenient if there are separate classes of entities, and a separate class of the repository for reading - teran
  • @teran changed in the parent class and in the child, the method is added to the parent, how to inherit this method and implement it? - Vladimirs Alesejevs
  • one
    tableName make protected and specify its value in the designer of books. and readAll from books at all, then it will be inherited, not blocked. - teran
  • one
    fields you should probably see in which thread the array $properties = ['sku' => null , 'name' => null] to render. thus, in the base class, define common properties and add new properties in the child class. + implement __get/__set methods (in the base) to access them. As for the interfaces, they are not needed for this task. - teran

0