0

这是我的第一个代码&有一个错误:

可捕获的致命错误:传递给 Shop::__construct() 的参数 1 必须是 Generator 的实例,没有给出,在第 7 行的 C:\xampp\htdocs\oop\config.php 中调用并在 C:\xampp\htdocs 中定义\oop\shop.class.php 在第 8 行

第 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;
        }
    }
}

配置文件:

include("system.class.php");
include("shop.class.php");

$generator = new Generator;
$shop = new Shop;

系统类

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;
    }
}

我做错了什么?谢谢 ;)

4

2 回答 2

3

尝试这个:

include("system.class.php");
include("shop.class.php");

$generator = new Generator;
$shop = new Shop($generator);

shop 类的构造签名是类型提示参数,这意味着您只能传入一个 Generator 对象。

于 2013-04-17T16:39:26.807 回答
2

您没有将任何参数传递给构造函数:

$generator = new Generator();
$shop = new Shop($generator);
于 2013-04-17T16:40:15.550 回答