我遇到了一个问题:
致命错误: 调用未定义的方法 ShopProductWriter::getProducer() 在 C:\OSPanel\域\zandstra.com\索引.php第 37 行
是什么原因造成的?
<?php
class ShopProduct {
public $title;
public $producerSurName;
public $producerFirstName;
public $price = 0;
public function __construct (
$title,
$firstName,
$surName,
$price
)
{
$this->title = $title;
$this->producerFirstName = $firstName;
$this->producerSurName = $surName;
$this->price = $price;
}
public function getProducer () {
return $this->producerFirstName . " " . $this->producerSurName;
}
}
$product1 = new ShopProduct (
"My Antonia",
"Willa",
"Cather",
"5.99"
);
class ShopProductWriter {
public function write ($shopProduct){
$str=$shopProduct->title . ":" . $shopProduct->getProducer()
. " (".$shopProduct->price.")\n";
print $str;
}
}
$product1 = new ShopProductWriter("My Antonia", "Willa", "Cather", 5.99);
$writer = new ShopProductWriter();
$writer->write($product1);
回首忆惘然