学习总结
- 后期静态绑定可以把定义的静态方法与调用它的类进行绑定,而不是定义它的类
- 构造方法是在类被实例化的时候由系统自动执行的
- 属性拦截器可以在属性被访问,设置,检测,删除时判断,如果没有权限或者不存在该属性则触发对应的方法。
- 方法和静态方法拦截器是在方法被访问时,如果不存在在或者没有权限访问,则触发对应的方法。
1.后期静态绑定
<?php
//后期静态绑定:就是在调用静态方法时,动态绑定到调用它的类上,而不是定义化的类
abstract class School
{
public static function printSDC(){
$sdcInfo = new static();//创建一个与当前调用类绑定的实例
echo '班名:',$sdcInfo->classes,'<br>';
echo '系名:',$sdcInfo->department,'<br>';
echo '校名:',$sdcInfo->school,'<br>';
echo '<hr>';
}
}
class Qhdx extends School
{
protected $school;
protected $department;
protected $classes;
public function __construct()
{
$this->school = '清华大学';
$this->department ='计算机系';
$this->classes = '04-01';
}
}
class Rmdx extends School
{
protected $school;
protected $department;
protected $classes;
public function __construct()
{
$this->school = '人民大学';
$this->department ='工商管理系';
$this->classes = '08-03';
}
}
Qhdx::printSDC();
Rmdx::printSDC();
- 运行效果图

2.构造函数和属性方法拦截器
<?php
//类中的构造方法,在实例化的时候调用
class School
{
private $school;
private $department;
private $classes;
//在类被实例化时自动触发
public function __construct()
{
$this->school = '清华大学';
$this->department ='计算机系';
$this->classes = '04-01';
}
public function printSDC()
{
echo '班名:',$this->classes,'<br>';
echo '系名:',$this->department,'<br>';
echo '校名:',$this->school,'<br>';
echo '<hr>';
}
public static function test1()
{
echo __METHOD__,'静态方法存在','<br>';
}
//类中的属性拦截器
//访问的类属性 不存在或者没有访问权限时触发此函数
public function __get($name)
{
if(isset($this->$name)):
echo '没有权限访问',$name,'属性','<br>';
else:
echo '不存在',$name,'属性','<br>';
endif;
}
//设置的类属性 不存在或者没有访问权限时触发此函数
public function __set($name, $value)
{
if($name === 'school'):
echo '不能修改学校名称','<br>';
else:
$this ->$name = $value;
endif;
}
//检测的类属性 不存在或者没有访问权限时触发此函数
public function __isset($name)
{
if(isset($this->$name)):
echo '没有权限检测',$name,'属性','<br>';
else:
echo '不存在',$name,'属性','<br>';
endif;
}
//删除的类属性 不存在或者没有访问权限时触发此函数
public function __unset($name)
{
if(isset($this->$name)):
echo '没有权限访问',$name,'属性','<br>';
else:
echo '不存在',$name,'属性','<br>';
endif;
}
//类中的方法拦截器
//调用类的普通方法 不存在或者没有访问权限时触发此函数
public function __call($name, $arguments)
{
echo '您访问的',__CLASS__,'::',$name,'方法不存在','<br>';
}
//调用类的静态方法 不存在或者没有访问权限时触发此函数
public static function __callStatic($name, $arguments)
{
echo '您访问的',__CLASS__,'::',$name,'静态方法不存在','<br>';
}
}
$school = new School();
$school -> printSDC();
//因为使用了属性拦截器所以类外调用时不会报错
echo $school ->school;//访问存在的私有属性
echo $school ->school1;//访问不存在的私有属性
$school ->school = '人民大学';//属性设置拦截器中限制不能修改学校名称
$school ->department = '工商管理系';//属性设置拦截器中进行设置
$school -> printSDC();
isset($school->school); //因为school是私有属性,所以检测它是触发了__isset()方法
isset($school->school1);
$school -> printSDC();
$school -> printSDC1();//printSDC1并没有在类中定义,所以触发了__call()方法
School::test1();//访问一个存在的静态方法
School::test2();//访问一个不存在的静态方法 触发了静态方法__callStatic()
?>
- 运行效果图

3.数据查询构造器实例
<?php
//连接mysql服务器
//创建一个抽象类,用来连接数据库
abstract class Conn
{
protected $host= 'localhost';
protected $userName= 'root';
protected $passWord= 'root';
protected $dbName;
protected $conn;
public function __construct($dbName)
{
//构造函数中传入数据库的名称
$this ->dbName = $dbName;
//构造方法中连接数据库
$this ->connect();
}
//数据库连接函数
public function connect()
{
$host = $this ->host;
$userName = $this ->userName;
$passWord = $this ->passWord;
$dbName = $this ->dbName;
$this ->conn = mysqli_connect($host,$userName,$passWord,$dbName);
//设置数据库连接的查询字符集,为utf8
mysqli_query($this ->conn,'set names utf8');
}
}
class Query extends Conn
{
protected $table;
protected $field;
protected $where;
protected $limit;
public function table($table)
{
$this ->table =$table;
return $this;//返回一个对象
}
public function field($field)
{
$this ->field =$field;
return $this;
}
public function where($where)
{
$this ->where =$where;
return $this;
}
public function limit($limit)
{
$this ->limit = $limit;
return $this;
}
public function getSql()
{
//生成查询字符串
return sprintf('select %s from `%s` where %s limit %s',$this->field,$this->table,$this->where,$this->limit);
}
public function select()
{
//在数据库中查询数据
$res = mysqli_query($this->conn,$this->getsql());
//把查询结果放在一个二维数组中
foreach($res as $info):
$rows[]=$info;
endforeach;
//返回二维数据
return $rows;
}
}
class DB
{
//使用静态方法拦截器
public static function __callStatic($name, $arguments)
{
//为db_pursey数据库创建一个连接
$conn = new Query('db_pursey');
//call_user_func把第一个参数做为回调函数调用,其于为回调函数的参数
return call_user_func([$conn,$name],...$arguments);
}
}
//DB类中没有定义静态方法table(),触发静态方法拦截器__callStatic;
$res = DB::table('tb_info')->field('*')->where('type = "公寓信息"')->limit(3)->select();
foreach($res as $row):
echo '类型:',$row['type'],'<br>';
echo '标题:',$row['title'],'<br>';
echo '内容:',$row['content'],'<br>';
echo '联系人:',$row['linkman'],'<br>';
echo '电话:',$row['tel'],'<br>';
echo '发布日期:',$row['edate'],'<br>';
echo '<hr>';
endforeach;
?>
- 运行效果图
