我是php的OOP概念的新手。我已经创建了这样一个类
<?Php
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: Class 'ShopProduct' not found in line 11
实际上我正在从教程中做这个例子。有人告诉我错误的部分在哪里。我的确与教程完全相同。
答案 0 :(得分:2)
你还需要定义一个ShopProduct类:
class ShopProduct
{
public $title;
public $price;
public function __construct( $title, $value1, $value2, $price)
{
$this->title = $title;
$this->price= $price;
}
}
答案 1 :(得分:1)
您创建了ShopProduct类的新实例,尽管您没有定义它。您只声明了ShopProductWriter而不是ShopProduct。这就是$writer = new ShopProductWriter();
有效且$product1 = new ShopProduct( "My Antonia", "Willa", "Cather", 5.99 );
不起作用的原因。