this is my first time asking a question here, so don't judge too harshly.
The logic behind this code is to add specific type of product to database. There are only three specific product classes with getters and setters for distinct product properties and insert()
method, a class with display and delete pruduct methods, and an abstract class with geters and setters for general product properties, that are included at config.php
.
The problem (only for one of the types of products all properties are added to the database, for others only the main ones - sku
, name
and price
) lies precisely in this code, since display and deletion work.
Note: I cannot refuse setters and getters, just as I cannot use conditional statements to handle the product type.
Second and maybe classic question: what's wrong with this code and how to fix it?
<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once 'config.php'; // here are the classes
function getClassname() {
$prod = $_POST['type'];
return new $prod();
}
$methods = [
'setSize' => $_POST['size'],
'setWeight' => $_POST['weight'],
'setHeight' => $_POST['height'],
'setWidth' => $_POST['width'],
'setLength' => $_POST['length']
];
function insertData(array $func)
{
$product = getClassname();
$product->dbConnect();
$product->uniqueSku();
$product->setSku($_POST['sku']);
$product->setName($_POST['name']);
$product->setPrice($_POST['price']);
foreach ($func as $m => $arg) {
if(method_exists($product, $m)){
$reflect = new ReflectionMethod($_POST['type'], $m);
$reflect->invoke($product, $arg);
//$product->$m($arg); doesn't help
} else {
break;
}
}
$product->insert();
}
insertData($methods);
//header('Location: ...');
p.s. I know that some code snippets look weird