传递给Shop::_construct()的PHP OOP参数1必须是
这是我的第一个代码&出现错误: 可捕获的致命错误:传递给Shop::uu construct()的参数1必须是Generator的一个实例,没有给定,在第7行的C:\xampp\htdocs\oop\config.php中调用,在第8行的C:\xampp\htdocs\oop\Shop.class.php中定义 第8行:传递给Shop::_construct()的PHP OOP参数1必须是,php,oop,Php,Oop,这是我的第一个代码&出现错误: 可捕获的致命错误:传递给Shop::uu construct()的参数1必须是Generator的一个实例,没有给定,在第7行的C:\xampp\htdocs\oop\config.php中调用,在第8行的C:\xampp\htdocs\oop\Shop.class.php中定义 第8行: public function __construct(Generator $generator) 我的代码: class Shop { private $g
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;
系统类
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;
}
}
我做错了什么?
谢谢;) 试试这个:
include("system.class.php");
include("shop.class.php");
$generator = new Generator;
$shop = new Shop($generator);
shop类的构造签名是参数,这意味着您只能传入生成器对象。尝试以下操作:
include("system.class.php");
include("shop.class.php");
$generator = new Generator;
$shop = new Shop($generator);
shop类的构造签名是参数,这意味着您只能传入生成器对象。您没有向构造函数传递任何参数:
您没有向构造函数传递任何参数:
这种结构有许多缺陷。你以前和C一起工作过吗?您定义的参数错误,实例化的类错误。我的第一个代码是
echo“Hello World”
@Allendar:Huh?对我来说,代码看起来不错,只是耦合得太紧了。他确实在$this->Generator
上犯了一个错误,应该是$this->Generator
。这个结构有很多缺陷。你以前和C一起工作过吗?您定义的参数错误,实例化的类错误。我的第一个代码是echo“Hello World”
@Allendar:Huh?对我来说,代码看起来不错,只是耦合得太紧了。他确实在$this->Generator
上犯了一个错误,应该是$this->Generator
。