批改状态:未批改
老师批语:
匿名对象和匿名类的实现过程
class BadPerson
{
private $name = '西门大官人';
public function story($name)
{
return $this->name.'喜欢上了:'.$name.'<br>';
}
}
//有三种方式来访问story()
//1.对象的方式
$BadPerson = new BadPerson();
echo $BadPerson->story('潘金莲').'<hr>';
//2.匿名对象
echo (new BadPerson())->story('绿茶').'<hr>';
//3.匿名类,php7以上才可以使用
echo (new class{
private $name = '西门大官人';
public function story($name)
{
return $this->name.'喜欢上了:'.$name.'<br>';
}
})->story('绿茶123').'<hr>';
//匿名类的三种应用场景
//1.匿名类中可以使用构造法方法
echo (new class('欧阳克'){
private $name;
//匿名类中的构造方法
public function __construct($name)
{
$this->name = $name;
}
public function story($name)
{
return $this->name.'喜欢上了:'.$name.'<br>';
}
})->story('灭绝师太').'<hr>';
//2.在匿名类中可以继承其他类中的成员
class KungFu
{
protected $kill; //招数
public function __construct($art= '')
{
$this->kill = $art;
}
public function show()
{
return $this->kill ? :'黯然销魂掌';
}
}
echo (new class('西门大官人') extends KungFu { //这里传入第二个参数,就会走第二个参数的值(下面有给默认值)
private $name;
//匿名类中的构造方法
public function __construct($name,$art='')
{
parent::__construct($art);
$this->name = $name;
}
public function story($name)
{
return $this->name.'喜欢上了:'.$name.'<br>';
}
public function show()
{
return $this->name.'的绝招是:'.parent::show().'<br>';
}
})->show().'<hr>';
//3.可以在类声明中嵌套一个匿名类
class Anmel
{
public $name = '狗';
protected $color = '黑色';
private $type = '哈士奇'; //私有属性
protected function info()
{
return '售价3000元';
}
public function demo1()
{
//宿主类中的私有成员不能再匿名类中直接使用
//可以通过在匿名类中创建一个构造方法将宿主类中的私有成员进行注入
//3.将宿主类中的私有属性作为匿名类的构造方法的参数传入即可
return (new class ($this->type) extends Anmel {
//1.在匿名类中创建一个属性来接收宿主类中的私有属性
private $type;
//2.创建一个构造方法
public function __construct($type)
{
$this->type = $type;
}
public function demo2()
{
return '我是嵌套匿名类中的方法:'.__METHOD__;
}
public function show()
{
return '动物名称是:'.$this->name.',品类是:'.$this->type.'.颜色是:'.$this->color;
}
});
}
}
//访问匿名类中的demo2()
echo (new Anmel())->demo1()->demo2();
echo (new Anmel())->demo1()->show();2.trait类的声明与工作原理;
if(!class_exists('Person')){
class Person
{
protected $name;
public function __construct($name='小明')
{
$this->name = $name;
}
public function study($course='php')
{
return $this->name.'在学习'.$course.'<hr>';
}
}
}
//创建一个trait特性类,可以使用类的语法
trait Course
{
public $frient = '小华';
public function study($name = '踢足球')
{
return $this->name.'在学习'.$name;
}
}
trait Recreation
{
public $friend = '晓军';
public function study($name='打篮球')
{
return $this->name.'和'.$this->friend.$name;
}
}
//问题1:父类Person与trait类Course之间的关系是什么?
//trait 类位于Person与Student之间(父类和子类之间)
class Student extends Person
{
// use Course; //导入一个trait类
// use Recreation;
use Course,Recreation
{
Course::study insteadof Recreation; //course中study替换了recreation
Recreation::study as MySport; //recreation 中的study改的别名MySport(因为和course中的study重名)
}
}
$student = new Student();
echo $student->study().'<hr>';
//echo $student->sport(); //从trait中拿出的数据
echo $student->MySport();3.类的自动加载函数的写法
spl_autoload_register(function ($className){
// require './class/'.$className.'php';
//存在命名空间的情况下
$className = str_replace("\\",'/',$className);
require './class/'.$className.'php';
});
echo Demo1::CLASS_NAME; //查看类名
echo Demo2::CLASS_NAME;4.对象的序列化和反序列化
class Db
{
//连接参数返回值
public $db = null;
public $host;
public $user;
public $pass;
//构造函数
public function __construct($host='127.0.0.1',$user='root',$pass= 'root')
{
$this->host=$host;
$this->user=$user;
$this->pass=$pass;
//确保实例化对象的时候能自动个链接数据库
$this->connect();
}
//数据库连接
private function connect()
{
$this->db = mysqli_connect($this->host,$this->user,$this->pass);
}
//对象序列化的时候自动调用
public function __sleep()
{
return ['host','user','pass'];
}
//反序列化
public function __wakeup()
{
$this->connect();
}
}
$obj = new Db();
/*
* 对象序列化的特点:
* 1.只保存对象中的属性,不保存方法;
* 2.只保存类名,不保存对象名;
*/
echo serialize($obj).'<hr>';
$tmp = serialize($obj);
var_dump(unserialize($tmp));
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号