I got feedback from an interview test where they wanted me to prove that I've got a grasp of OOP php programming. This file especially I got feedback on that it's more functional than it is Object-Oriented so I'm asking all the PHP gurus if they have any tips on how I can turn this into more of an OO component.
The first file:
<?php
require_once('includes/initialize.php');
require_once('productClass.php');
header('Content-Type: application/json; charset=UTF-8');
$response = array();
if (isset($_POST['sku']) && isset($_POST['name']) && isset($_POST['price']) && isset($_POST['type']) && isset($_POST['description'])) {
$class = $_POST['type'];
$product = new $class($_POST['sku'], $_POST['name'], $_POST['price'], $_POST['type'], $_POST['description']);
$sku = $product->formatSKU($product->getType(), $product->getSKU());
$name = $product->getName();
$price = $product->getPrice();
$type = $product->getType();
$description = $product->formatDescription($product->getDesc());
$result = $api->addProduct($sku, $name, $price, $type, $description);
if ($result) {
header("HTTP/1.1 200 OK");
header("Location: https://scandiweb-product-page.herokuapp.com/");
exit();
} else {
header("HTTP/1.1 406 Error inserting product");
}
} else {
header("HTTP/1.1 499 Required parameters missing");
echo 'Required parameters missing';
}
This is the product class file:
<?php
abstract class Product
{
private $prodSku;
private $prodName;
private $prodPrice;
private $prodType;
private $prodDesc;
public function __construct($prodSku, $prodName, $prodPrice, $prodType, $prodDesc)
{
$this->prodSku = $prodSku;
$this->prodName = $prodName;
$this->prodPrice = $prodPrice;
$this->prodType = $prodType;
$this->prodDesc = $prodDesc;
}
public function getSKU()
{
return $this->prodSku;
}
public function getName()
{
return $this->prodName;
}
public function getPrice()
{
return $this->prodPrice;
}
public function getType()
{
return $this->prodType;
}
public function getDesc()
{
return $this->prodDesc;
}
public function formatSKU($inputType, $inputSKU)
{
return strtoupper(substr($inputType, 0, 4)) . str_pad($inputSKU, 4, "0", STR_PAD_LEFT);
}
abstract protected function formatDescription($inputDesc);
}
class DVD extends Product
{
public function formatDescription($inputDesc)
{
return "Size: " . $inputDesc . "MB";
}
}
class Furniture extends Product
{
public function formatDescription($inputDesc)
{
$implodedInput = implode('x', $inputDesc);
return "Dimensions: " . $implodedInput;
}
}
class Book extends Product
{
public function formatDescription($inputDesc)
{
return "Weight: " . $inputDesc . "kg";
}
}
Their feedback was I needed to utilize getters & setters, any idea how can this be achieved? any help is appreciated.