define('SITE_NAME','PHP中文网');
class User1
{
//声明属性
private $siteName = SITE_NAME;
private $name = '老顽童';
private $email = 'lwt@php.cn';
private $course = ['php','java','python'];
const LECTURE = '朱老师';
//构造方法
public function __construct($name='',$email='', $siteName='',array $course=[])
{
//如果传参,则使用新值初始化属性,否则使用默认值
$name ? ($this->name = $name) : $this->name;
$email ? ($this->email = $email) : $this->email;
$siteName ? ($this->siteName = $siteName) : $this->siteName;
$course ? ($this->course = $course) : $this->course;
}
//查询器
public function __get($name)
{
return $this->$name;
}
//设置器
public function __set($name,$value)
{
return $this->$name = $value;
}
//在类中访问类常量,使用self来引用当前类名
public function getConst()
{
//类内部也可以直接使用当前类名
// return User1::LECTURE;
//推荐使用self:当类名改变时,不必修改内部对它的引用
return self::LECTURE;
}
}