批改状态:合格
                        老师批语:
                    
                            匿名类的操作实例
/**
 * 匿名类:
 * 1. php 7.0+ 才支持
 * 2. 类似于匿名函数,就是没有名称的类
 * 3. 匿名类适合于一次性的创建与引用
 * 4. 匿名类总是与: new 配套使用
 */
class BadPerson
{
    private $name = '奥巴马';
    public function story($name)
    {
        return $this->name.'美国的总统是: <span style="color:red">'.$name.'</span>';
    }
}
//有三种方式来访问 story()
//1. 对象
$badPerson = new BadPerson();
echo $badPerson->story('葫芦娃'), '<hr>';
//2.匿名对象
echo (new BadPerson())->story('桃太郎'), '<hr>';
//3.匿名类
echo (new class {
    private $name = '蜘蛛侠';
    public function story($name)
    {
        return $this->name.'打了: <span style="color:red">'.$name.'</span>';
    }
})->story('康熙'), '<hr>';
// 匿名类的三种应用场景
//1. 匿名类中可以使用构造方法
echo (new class ('菜鸟一个'){
    private $name;
    // 匿名类中的构造方法
    public function __construct($name)
    {
        $this->name = $name;
    }
    public function story($name)
    {
        return $this->name.'看上了: <span style="color:red">'.$name.'</span>';
    }
})->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.'喜欢上了: <span style="color:red">'.$name.'</span>';
    }
    public function show()
    {
        return $this->name.'的绝招是: '.'<span style="color:red">'.parent::show().'</span>';
    }
})->show(), '<hr>';
//3.可以在类声明中嵌套一个匿名类
class Anmal   // 宿主类, 父类的角色
{
    public $name = '狗';
    protected $color = '黑色';
    private $type = '哈士奇';
    protected function info ()
    {
        return '市.场售价3000元';
    }
    public function demo1()
    {
        // 宿主类中的私有成员不能在匿名类中直接使用
        // 可以通过在匿名类创建一个构造方法将宿主类中的私有成员进行注入
        // 3. 将宿主类中的私有属性做为匿名类的构造方法的参数传入即可
        return (new class ($this->type) extends Anmal {
            //1. 在匿名类中创建一个属性用来接收宿主类中的私有属性
            private $type;
            //2. 创建一个构造方法
            public function __construct($type)
            {
                $this->type = $type;
            }
            public function demo2()
            {
                return '我是嵌套匿名类中的方法: '. __METHOD__;
            }
            public function show()
            {
                return
                    '动物的名称是: ' .$this->name. '<br>'.
                    '动物的颜色是: ' .$this->color. '<br>'.
                    '动物的品.种是: ' .$this->type. '<br>';
            }
        });
    }
}
// 访问匿名类中的 demo2()
echo (new Anmal())->demo1()->demo2();
echo '<hr>';
echo (new Anmal())->demo1()->show();点击 "运行实例" 按钮查看在线实例
Trait的实例
<?php
if (!class_exists('Person')) {
    class Person
    {
        protected $name;
        public function __construct($name='泰利')
        {
            $this->name = $name;
        }
        public function study($course='php')
        {
            return $this->name . '在培训: ' . $course;
        }
    }
}
// 创建一个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与triat类Course之间的关系?
// trait 类位于 Person 与 Student之间
class Student extends Person
{
//    use Course;  // 导入了一个trait 类
//    use Recreation;
    use Course, Recreation {
        Course::study insteadof Recreation;
        Recreation::study as MySport;
    }
}
$student = new Student();
echo $student->study(), '<hr>';
echo $student->MySport(), '<hr>';点击 "运行实例" 按钮查看在线实例
3.类的自动加载方法实例
<?php
/**
 * 类的自动加载
 */
//include './class/Demo1.php';
//include './class/Demo2.php';
spl_autoload_register(function ($className){
    require './class/'.$className.'.php';
//    $className = str_replace("\\","/", $className);
//    require './class/'.$className.'.php';
});
echo Demo1::CLASS_NAME, '<hr>';
echo Demo2::CLASS_NAME, '<hr>';点击 "运行实例" 按钮查看在线实例
4.对象的序列化
<?php
/**
 * 对象的序列化
 */
//$num = 500;
//echo serialize($num),'<hr>'; // i:500;
//$name = 'peter';
//echo serialize($name), '<hr>'; // s:5:"peter";
//$course = ['html','css','javascript'];
//echo serialize($course), '<hr>';
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));点击 "运行实例" 按钮查看在线实例
5.
                Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号