php - 返回用户姓名和姓氏的方法以及根据用户是否成人返回布尔值的方法
问题描述
<?php
class User {
var $id;
var $first_name;
var $last_name;
var $age;
function construct($id,$first_name,$last_name,$age){
$this>=$id;
$this>=$first_name;
$this>=$last_name;
$this>=$age;
}
public function getFirstName(){
return $this->last_name;
}
public function getLastName(){
return $this->last_name;
}
function values(){
echo "User id: {$this->id} <br/> First name : {$this->fname} <br/> Last name : {$this ->lname}";
}
function setAge($age){
if(is_numeric($age) && $age >= 18){ $this->$age = $age;
}
else { throw new Exception("Age is not valid"); }
}
}
$user = new User (001,'Vladmir','Bozic');
$lastName = $user->getLastName();
$firstName = $user->getFirstName();
?>
/*我设法更改了代码行,但它没有写在我的页面上。我在浏览器中打开时什么都没有。在我的任务中,我需要对构造函数进行分类,这是一个返回用户姓名和姓氏的方法,一个根据用户是否是成年人返回布尔值的方法。
解决方案
<?php
class User {
public $id;
public $first_name;
public $last_name;
public $age;
public function __construct($id, $first_name, $last_name, $age){
$this->id = $id;
$this->first_name = $first_name;
$this->last_name = $last_name;
$this->age = $age;
}
public function name(){
echo "ID: <br />" .$this->id. " Ime : <br />" .$this->first_name. " Prezime : <br />" .$this->first_name;
}
public function age(){
if ($this->age >=18){
echo "Punoljetan(18+)";
}
else {
echo "Maloljetan (18-)";
}
}
$klijent = new User(1,'Vladmir','Bozic', 12);
$klijent -> name();
$klijent -> age();
echo "<br />";
echo "<br />";
echo "<br />";
echo "<br />";
$klijent = new User(2,'Nevenko','Markovic', 25);
$klijent -> name();
$klijent -> age();
?>
/* 我不断收到错误,我不知道是什么问题?
推荐阅读
- c++ - CMake 也将所有 gcc 标志传递给 nvcc
- flutter - 如何基于 json 响应创建动态小部件
- typo3 - 8.7 LTS 与 9.5 LTS 中的 Cookie 行为/默认 cookie
- java - 使布局适应所有屏幕分辨率 - Android
- html - 如何在 HTML 输入表单中验证尼泊尔语或印地语单词
- reporting-services - SSRS 数据集取决于其他数据集
- python-3.x - 如何在 python 中换行(Python 3)
- node.js - Winston - 多控制台传输
- azure - 使用 deploy.cmd [Windows 机器] 在 azure app 服务上部署 laravel
- reactjs - react-fullpage 右导航切换丢失