這要求真奇怪,我這哪里錯了?
<?php
//定義一個Car類并實例化一個對象
class Car{
? ? //設(shè)置屬性
? ? private $name = '汽車';
? ? private $price = 1000;
? ? //構(gòu)造器
? ? public function __construct($name, $price)
? ? {
? ? ? ? $this->name = $name;
? ? ? ? $this->price = $price;
? ? }
? ? //設(shè)置訪問器
? ? public function getName()
? ? {
? ? ? ? return $this->name;
? ? }
? ??
? ? public function getPrice()
? ? {
? ? ? ? return $this->price;
? ? }
? ? //設(shè)置更改器
? ? 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;//調(diào)用__toString函數(shù)
2020-04-28
類名重定義,把類名Car換個其他名字,或者定義類的時候首字母盡量小寫,避免與庫中的類重復(fù)
2019-01-26
自 PHP 5.2.0 起,如果將一個未定義?__toString()?方法的對象轉(zhuǎn)換為字符串,會產(chǎn)生?
E_RECOVERABLE_ERROR
?級別的錯誤 ----https://www.cnblogs.com/toumingbai/p/9364982.html2018-08-17
echo $car;//調(diào)用__toString函數(shù) 為什么這樣能輸出數(shù)據(jù)。
? public function __construct($name, $price)
? ? {
? ? ? ? $this->name = $name;
? ? ? ? $this->price = $price;
? ? }
這個干什么,有什么作用。
2018-08-14
沒什么錯啊? ?最后輸出?