3 回答

TA貢獻1825條經驗 獲得超6個贊
你很接近。將變量、參數和參數命名為有意義的名稱。
如果它大于 130 或小于 0,則將 $age 值設置為未設置(就像字符串一樣)
大概意思 $this->age = 'unset';
<?php
class User
{
private $name;
private $age;
public $site;
function __construct($name, $site, $age)
{
echo "User was successfully created!";
$this->name = $name;
$this->site = $site;
if ($age > 130 || $age < 0) {
$this->age = 'unset';
} else {
$this->age = $age;
}
}
public function getFullInfo()
{
return "$this->name at the age of $this->age is a user of $this->site";
}
}
$user = new User('Tony McTony', 'Tree House', 35);
echo $user->getFullInfo();
// Output: User was successfully created!Tony McTony at the age of 35 is a user of Tree House

TA貢獻1839條經驗 獲得超15個贊
我認為這里是:
return "$this->name at the age of $this->age is a user of $this->site";
// it should be : (i think)
return $this->name . "at the age of " . $this->age . " is a user of " . $this->site;
希望能幫助到你
- 3 回答
- 0 關注
- 184 瀏覽
添加回答
舉報