![](/img/trans.png)
[英]Catchable Fatal Error: Argument 1 passed to AppBundle\Form\TagType::__construct() must be an instance of Doctrine\ORM\EntityRepository, none given,
[英]Catchable fatal error: Argument 1 passed to getPrice() must be an instance of Rectangle, none given
我只是想计算矩形类以外的矩形区域的价格,该矩形类将其返回。 只是尝试一个多态的例子。 难道我做错了什么? 我是OOP的新手。 这是我的代码:
// getting the area
class Rectangle{
public $height;
public $width;
public function __construct($height, $width){
$this->height = $height;
$this->width = $width;
}
public function getArea(){
return $this->height * $this->width. "<br />";
}
}
在类之外,我创建了函数`getPrice()
`function getPrice(Rectangle $rectangle){
return $rectangle->getArea() * 0.25;
}
创建实例并打印出来。
$rectangle = new Rectangle(12, 15);
echo getPrice();
显示的错误是:
可捕获的致命错误:传递给getPrice()的参数1必须是Rectangle的实例,没有给出,在第41行的/var/www/html/crashcourse/oop_php/phpacademy/polymorphism.php中调用并在/ var / www /中定义第31行的html / crashcourse / oop_php / phpacademy / polymorphism.php
看起来getPrice()函数具有Rectangle类类型的$矩形参数,您需要传递实例化的Rectangle对象。 进行如下操作:
$ rectangle = new Rectangle (12, 15);
echo getPrice ($ rectangle);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.