首页 > 解决方案 > 返回用户姓名和姓氏的方法以及根据用户是否成人返回布尔值的方法

问题描述

<?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();
?>

/*我设法更改了代码行,但它没有写在我的页面上。我在浏览器中打开时什么都没有。在我的任务中,我需要对构造函数进行分类,这是一个返回用户姓名和姓氏的方法,一个根据用户是否是成年人返回布尔值的方法。

标签: phpclass

解决方案


<?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();
?>

/* 我不断收到错误,我不知道是什么问题?


推荐阅读