创建类
// 1. 创建类
class Demo1
{
// 2. 添加类成员
public $site = 'php中文网';
public $role = '讲师';
public function getSite()
{
return $this->site . ' 欢迎您~~';
}
public function getRole()
{
return $this->role;
}
}
// 3. 访问类成员
$obj = new Demo1();
echo $obj->site . '<br>';
echo $obj->getRole() . '<br>';

构造函
// 1. 创建类
class Demo3
{
// 2. 添加类成员
public $site;
public $role;
public function getInfo()
{
return '我是: ' . $this->site . $this->role;
}
// 构造函
public function __construct($site, $role)
{
// 1. 初始化类成员
$this->site = $site;
$this->role = $role;
echo $this->getInfo();
}
}
// 3. 访问类成员
new Demo3('PHP中文网', '张三');

访问控制,实现类的封装
// 访问控制,实现类的封装
// 1. 创建类
class Demo4
{
// 2. 添加类成员
public $site;
protected $role;
// 构造方法
public function __construct($site, $role)
{
$this->site = $site;
$this->role = $role;
}
// 魔术方法: __get($name), 属性重载
public function __get($name)
{
// 仅允许用户名是'admin'的用户访问,其它访问返回: 无权访问
$username = $_GET['username'] ?? '';
if (isset($username) && $username === 'admin') {
return isset($this->$name) ? $this->$name : '属性未定义';
} else {
return '无权访问';
}
}
}
// 3. 访问类成员
$obj = new Demo4('www.php.cn', '讲师');
echo $obj->role;
echo '<br>';
echo $obj->name;

类的继承
// 类的继承
// 1. 创建类
class Demo5
{
// 2. 添加类成员
public $site;
protected $role;
public function getInfo()
{
return '我是: ' . $this->site . '讲师: ' . $this->role;
}
public function __construct($site, $role)
{
$this->site = $site;
$this->role = $role;
}
}
class Demo5_1 extends Demo5
{
// 当前子类的自身属性
private $course;
public function __construct($site, $role, $course)
{
// parent:: 调用 父类中的成员
parent::__construct($site, $role);
$this->course = $course;
}
// 重写父类中的方法
public function getInfo()
{
return parent::getInfo() . ', 负责的课程是: ' . $this->course;
}
}
// 3. 访问类成员
$sub = new Demo5_1('php中文网', '张三', 'php');
echo $sub->getInfo();

trait: 创建一个公共方法库
// trait: 当成一个公共方法库
// trait: 使用了类的定义的语法,但不是类, 所以不能实例化
trait Test
{
public function getInfo()
{
return '我是: ' . $this->site . '讲师: ' . $this->role;
}
}
// 1. 创建类
// Demo6: 宿主类
class Demo6
{
// 导入trait类库
use Test;
// 2. 添加类成员
public $site;
protected $role;
public function __construct($site, $role)
{
$this->site = $site;
$this->role = $role;
}
}
class Demo6_1 extends Demo6
{
// 当前子类的自身属性
private $course;
public function __construct($site, $role, $course)
{
// parent:: 调用 父类中的成员
parent::__construct($site, $role);
$this->course = $course;
}
}
// 优先级: 当前类中的同名方法 > trait类中的同名方法 > 父类中的同名方法
// 3. 访问类成员
$obj = new Demo6('php中文网', '张三');
echo $obj->getInfo();
echo '<hr>';
$sub = new Demo6_1('php.cn', '张三', 'php');
echo $sub->getInfo();

接口
// 接口:
interface iDemo
{
// 接口方法
public function getInfo();
public function hello();
}
// 1. 创建类
class Demo7 implements iDemo
{
// 2. 添加类成员
public $site;
protected $role;
public function getInfo()
{
return '我是: ' . $this->site . '讲师: ' . $this->role;
}
public function hello()
{
return 'Hello 大家好';
}
// 构造方法
public function __construct($site, $role)
{
$this->site = $site;
$this->role = $role;
}
}
// 3. 访问类成员
$obj = new Demo7('php中文网', '张三');
echo $obj->getInfo() . '<br>';
echo $obj->hello() . '<br>';

抽象类
// 抽象类
abstract class Chouxiang
{
// 抽象方法
abstract public function getInfo();
// 已实现方法
public function hello()
{
return 'Hello 大家晚上好';
}
}
// 1. 创建类
class Demo8 extends Chouxiang
{
// 2. 添加类成员
public $site;
protected $role;
public function getInfo()
{
return '我是: ' . $this->site . '讲师: ' . $this->role;
}
// 构造方法
public function __construct($site, $role)
{
$this->site = $site;
$this->role = $role;
}
}
// 3. 访问类成员
$obj = new Demo8('php中文网', '张三');
echo $obj->getInfo() . '<br>';
echo $obj->hello() . '<br>';


