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 关注
- 147 浏览
添加回答
举报