1创建类、实例化、命名空间、类成员、类方法
<?php
namespace danyue;
class User{
public $uid = 1;
public $name = 'danyue';
public $age = 22;
public function index()
{
return "UID:".$this -> uid."<br>". "姓名:".$this -> name."<br>"."年龄:".$this -> age."<br>";
}
}
$user = new User();
echo $user ->name;
echo '<hr>';
echo $user -> index();
构造方法
<?php
class User
{
public $uid;
public $name;
public $age;
public function index()
{
return "UID:" . $this->uid . "<br>" . "姓名:" . $this->name . "<br>" . "年龄:" . $this->age . "<br>";
}
public function __construct($uid, $name, $age)
{
$this->uid = $uid;
$this->name = $name;
$this->age = $age;
}
}
$user = new User('2', 'Dony', '28');
echo $user->name;
echo '<hr>';
echo $user->index();