我从面试测试中得到反馈,他们想让我证明我掌握了OOP编程。这个文件--尤其是我收到的反馈--它比面向对象的功能更强大,所以我询问所有PHP专家,他们是否有任何关于我如何将其转化为OO组件的提示。
第一个文件:
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';
}这是产品类文件:
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";
}
}他们的反馈是我需要利用getter和setter,知道如何实现这一点吗?任何帮助都是非常感谢的。
发布于 2022-02-16 23:13:32
由于OOP的问题,您的代码过时了,很容易被误用。考虑一下,你让我来处理你的代码:
$dvd = new DVD("12345", "Your Name", "15 dollars", "Romance", "35MB");
echo($dvd->formatDescription("Not the description"));这表明很容易犯很多错误。我重复了一些不是描述( OOP问题)的内容,以及几个格式不正确的变量(可以通过现代PHP修复)。
下面是产品和DVD类的快速重写:
abstract class Product
{
public function __construct(
private readonly string $sku,
public readonly string $name,
public readonly int $price,
public readonly string $type,
protected readonly int|array $description
){}
public function formatSKU(): string
{
return strtoupper(substr($this->type, 0, 4)) . str_pad($this->sku, 4, "0", STR_PAD_LEFT);
}
abstract protected function formatDescription(): string;
}
class DVD extends Product
{
public function formatDescription(): string
{
return "Size: " . $this->description . "MB";
}
}
// Test it:
$dvd = new DVD("12345", "Your Name", 1500, "Romance", 35);
echo($dvd->formatSKU() . ' ' . $dvd->formatDescription());一些要点:
$this->variableName而不是冗余,并且可能错误地从类外部传入变量。$description是联合型 (PHP8.0)“int2000Array”,它显示了您对这个特定变量的设计是多么奇怪(因为它是一个由两个int组成的数组,用于家具类,另一个是int),所以这也是我要重构的东西。
我也不太确定$type.它真的是一根绳子吗?也许您可以使用PHP8.1中的枚举 (或多个)将其锁定为唯一应该存在的类型。因为类的所有成员都有相同的类型,所以传递它是没有意义的;相反,您可以在Product上创建一个抽象方法,然后在每个子类中对每个值(iirc)进行不同的实现。
对我来说,真正突出的是缺乏验证。您尝试确保设置了所有POST变量,但没有检查其中任何一个包含您想要的内容(例如,以美分为单位的正整数,这将避免浮点错误的可能性)。您假设您的前端将承担所有的任务,但它不能:用户可以随心所欲地提交一个帖子请求。特别是在重写之后,您需要验证,否则结果将是服务器错误。尤其要确保$class是您想要的类之一(我将使用PHP8中的match )。
此外,如果您对产品有任何控制,$api->addProduct($sku, $name, $price, $type, $description);可能会接受产品。
https://codereview.stackexchange.com/questions/274173
复制相似问题