这要求真奇怪,我这哪里错了?
<?php
//定义一个Car类并实例化一个对象
class Car{
//设置属性
private $name = '汽车';
private $price = 1000;
//构造器
public function __construct($name, $price)
{
$this->name = $name;
$this->price = $price;
}
//设置访问器
public function getName()
{
return $this->name;
}
public function getPrice()
{
return $this->price;
}
//设置更改器
public function setName($name)
{
$this->name = $name;
}
public function setPrice($price)
{
$this->price = $price;
}
//toString
public function __toString()
{
return '车名:'.$this->getName().",价格:".$this->getPrice().'<br>';
}
}
$car = new Car('法拉利',10E9);
echo $car;//调用__toString函数