1. 问答
  2. 问答详情

在PHP中定义一个类

1个回答

0

采纳

声明一个类,属性和方法显示了一个类的构成. 注意在大括号({})内你只能声明变量或者函数。

代码显示了如何在一个类中定义三个属性和两个方法。

class User extends AnotherClass
{
   //Access Variable Declaration
   public $name;
   public $password;
   public $lastLogin;
   
   //Access Function Declaration
   public function __construct($name,$password){
       $this->name = $name;
       $this->password = $password;
       $this->lastLogin = time();
   
   }
   // 获取最后访问的时间
  public function getLastLogin(){
      return(date("M d Y", $this->lastLogin));
  }
}

//创建一个对象的实例
$user = new User("Leon", "123456");
//获取最后访问的时间
print($user->getLastLogin() ."n");
//打印用户名
print("$user->name");


撰写答案

验证码
点击刷新