批改状态:未批改
老师批语:
<?php
//1.匿名类 PHP7+才支持
// 2.类似匿名函数,就是没有名称的类
// 3匿名类适合于一次性创建与使用
// 4匿名类总是和new配套使用
class BadPerson
{
private $name = '西门大官人';
public function story($name)
{
return $this->name.'喜欢上了:<span style="color:red">'.$name.'</span><br>';
}
}
//对象正常的用new实例化
$person = new BadPerson();
echo $person->story('金莲妹妹');
//简写,匿名对象
echo (new BadPerson())->story('灭绝师太');
//匿名累的使用,直接new类,直接在类中写属性和方法
echo (new class{
private $name = '西门大官人';
public function story($name)
{
return $this->name.'喜欢上了:<span style="color:red">'.$name.'</span><hr>';
}
})->story('武大郎');
//匿名类的三种应用场景
//1.匿名类中可以使用构造方法
echo (new class('段誉') {
private $name ;
public function __construct($name) //如果构造方法有参数,要在new class(参数)
{
$this->name = $name;
}
public function story($name)
{
return $this->name.'喜欢上了:<span style="color:red">'.$name.'</span><br>';
}
})->story('神仙姐姐');
//2.匿名类可以继承其他类中的成员
class Kungfu //父类功夫类
{
private $skill; //技能
public function __construct($act='')
{
$this->skill = $act;
}
public function show()
{
return $this->skill?:'降龙十八掌'; //$this->skill?$this->skill:'凌波微步'的简写
}
}
echo (new class('乔峰','凌波微步') extends Kungfu{ //子类继承父类
private $name ;
public function __construct($name,$act='')
{
parent::__construct($act); //再次构造父类构造方法
$this->name = $name;
}
public function story($name)
{
return $this->name.'喜欢上了:<span style="color:red">'.$name.'</span><br>';
}
public function show()
{
return $this->name.'的绝招是:<span style="color:red">'.parent::show().'</span><br>'; //调用父类的show()方法
}
})->show();
//3.可以在类声明中嵌套一个类(类种类)
class animal
{
public $name = '狗';
protected $color = '白色';
private $type = '金毛';
public function test()
{
return (new class( $this->type) extends animal{ //继承animal类
//访问宿主类中私有属性的方法:1.创建一个变量来接收传过来的私有属性
//2.创建构造函数 3.将宿主类中的私有变量作为参数传递进来
private $type;
public function __construct($type)
{
$this->type = $type;
}
public function testtest()
{
return '当前嵌套类的方法是'.__METHOD__.'<br>';
}
public function show()
{
return
'当前的动物是'.$this->name.'<br>'.
'当前的动物的颜色是'.$this->color.'<br>'.
'当前的动物的颜色是'.$this->type.'<br>';//不能直接访问父类中的私有属性,可以在匿名类中创建一个构造方法将宿主类中的私有成员进行注入
}
});
}
}
echo (new animal())->test()->testtest(); //(new animal())-test()返回的是匿名类,可以直接调用该匿名类下的方法
echo (new animal())->test()->show();点击 "运行实例" 按钮查看在线实例
Trait类
<?php
//php只能实现单继承,Trait打破了限制
// Trait是一种代码复用机制
// Trait使用类的语法,但不是类,不能实例化
// Trait相当于方法集
class Person
{
protected $name;
public function __construct($name='张三')
{
$this->name = $name;
}
public function study($course='PHP')
{
return $this->name.'在学习'.$course.'<hr>';
}
}
Trait Course
{
public $friend1 = '李四';
public function sport($sport='踢足球')
{
return $this->name.'在学习'.$sport.'<hr>';
}
//父类、Trait类、和子类的关系:Trait类位于父类和子类之间
public function study($sport='踢足球')
{
return $this->name.'在学习'.$sport.'<hr>';
}
}
Trait Hobby
{
public $friend2 = '王五';
public function study($sport='打乒乓球')
{
return $this->name.'和'.$this->friend2.'在'.$sport.'<hr>';
}
}
class Student extends Person
{
// use Course; //用use关键字来导入Trait
// use Hobby;
use Course,Hobby{ //Course和Hobby中有同名函数,直接引用会报错
Hobby::study insteadof Course; //将Course中的study代替Hobby中的
Course::study as MySport; //将Hobby中的Study起别名,可以用别用来调用
}
}
$student = new Student();
echo $student->study();
echo $student->MySport();点击 "运行实例" 按钮查看在线实例
类的自动加载spl_autoload_register()
<?php
// 类的自动加载spl_autoload_register()
// require 'class1.php';
// require 'class2.php';
spl_autoload_register(function($className){
require $className.'.php'; //$className要加载的类名
});
echo Class1::CLASS_NAME.'<hr>';
echo Class2::CLASS_NAME.'<hr>';点击 "运行实例" 按钮查看在线实例
对象的序列化serialize()和反序列化unserialize()
<?php
//序列化serialize(),反序列化unserialize()
$num = 1000;
echo serialize($num).'<br>'; //整数的序列化
$string = 'PHP';
echo serialize($string ).'<br>'; //字符串的序列化
$arr = ['PHP','HTML','JS'];
echo serialize($arr).'<hr>'; //数组的序列化
class Db
{
private $host;
private $user;
private $pass;
private $dn = null;
public function __construct($host='127.0.0.1',$user='root',$pass='123456')
{
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
//确保对象实例化是直接连接数据库
$this->connect();
}
private function connect()
{
$this->db = mysqli_connect($this->host, $this->user, $this->pass);
}
//__sleep()魔术方法,在对象序列化时调用,没有参数
public function __sleep()
{
return ['host','user','pass'];
}
//__wakeup()魔术方法,在对象反序列化时调用
public function __wakeup()
{
return $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号