这是我的第一个代码&有错误:
可捕获的致命错误:传递给Shop :: __ construct()的参数1必须是Generator的实例,没有给定,在第7行的C:\ xampp \ htdocs \ oop \ config.php中调用,并在C:\中定义第8行的xampp \ htdocs \ oop \ shop.class.php
第8行:
public function __construct(Generator $generator)
我的代码:
class Shop
{
private $generator;
private $vates;
public function __construct(Generator $generator)
{
$this->Generator = $generator;
$this->vates = 'Connected with 250 vates!';
}
public function connect()
{
if ($this->Generator->isDown())
{
echo 'Sorry, the generator is down!';
}
else
{
echo 'Sucessfully', $this->vates;
}
}
}
CONFIG.PHP:
include("system.class.php");
include("shop.class.php");
$generator = new Generator;
$shop = new Shop;
System.class
class Generator
{
private $power = false;
public function powerUp()
{
$this->power = true;
echo 'You powered up the generator';
}
public function shutDown()
{
$this->power = false;
echo 'The generator slowly shutting down...';
}
public function isDown()
{
return $this->power;
}
}
我做错了什么? 谢谢;)
答案 0 :(得分:3)
试试这个:
include("system.class.php");
include("shop.class.php");
$generator = new Generator;
$shop = new Shop($generator);
商店类的构造签名是type hinting参数,这意味着您只能传入Generator对象。
答案 1 :(得分:2)
您没有将任何参数传递给构造函数:
$generator = new Generator();
$shop = new Shop($generator);