Fatal error: Call to undefined method ShopProductWriter::getProducer()
Fatal error: Call to undefined method ShopProductWriter::getProducer()
我遇到了一个问题:
Fatal error: Call to undefined method ShopProductWriter::getProducer() in C:\OSPanel\domains\zandstra.com\index.php on line 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);
这里
$product1 = new ShopProductWriter("My Antonia", "Willa", "Cather", 5.99);
您重新创建了 $product1
,在那之前它是 ShopProduct
。由此,$product1
是一个 ShopProductWriter
对象。这没有 getProducer()
方法,只有 write()
方法。这是你代码中的一个错字,我认为你已经多次重写你的代码并且一些旧的初始化留在你应该离开的地方。在这种形式下,可能会做你想做的事:
<?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;
}
}
class ShopProductWriter {
public function write ($shopProduct){
$str=$shopProduct->title . ":" . $shopProduct->getProducer()
. " (".$shopProduct->price.")\n";
print $str;
}
}
$product1 = new ShopProduct("My Antonia", "Willa", "Cather", 5.99);
$writer = new ShopProductWriter();
$writer->write($product1);
我遇到了一个问题:
Fatal error: Call to undefined method ShopProductWriter::getProducer() in C:\OSPanel\domains\zandstra.com\index.php on line 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);
这里
$product1 = new ShopProductWriter("My Antonia", "Willa", "Cather", 5.99);
您重新创建了 $product1
,在那之前它是 ShopProduct
。由此,$product1
是一个 ShopProductWriter
对象。这没有 getProducer()
方法,只有 write()
方法。这是你代码中的一个错字,我认为你已经多次重写你的代码并且一些旧的初始化留在你应该离开的地方。在这种形式下,可能会做你想做的事:
<?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;
}
}
class ShopProductWriter {
public function write ($shopProduct){
$str=$shopProduct->title . ":" . $shopProduct->getProducer()
. " (".$shopProduct->price.")\n";
print $str;
}
}
$product1 = new ShopProduct("My Antonia", "Willa", "Cather", 5.99);
$writer = new ShopProductWriter();
$writer->write($product1);