批改状态:合格
老师批语:
匿名对象与匿名类的实现过程;
<?php
/**
* 匿名对象
* 匿名类
*/
//创建一个类
class Test_090501{
private $name = 'Andrew';
public function hobby($sport){
return $this->name.'喜欢'.$sport.'<br>';
}
}
//通过对象调用类的方法
$a = new Test_090501();
echo $a->hobby('踢足球');
//匿名对象的方式调用方法
echo (new Test_090501())->hobby('打篮球'),'<hr>';
//匿名类的使用
echo (new class {
private $name = 'Andrew';
public function hobby($sport){
return $this->name.'喜欢'.$sport.'<br>';
}
})->hobby('健身');
//匿名类访问构造方法
echo (new class ('a','b') {
private $name ;
private $hobby;
public function __construct($name,$hobby)
{
$this->name = $name;
$this->hobby = $hobby;
}
public function hobby(){
return $this->name.'喜欢'.$this->hobby.'<br>';
}
})->hobby();
class Test_090502{
protected $hobby;
public function __construct($hobby='')
{
$this->hobby = $hobby;
}
public function hobby(){
return $this->hobby ? :$this->hobby='滑雪'.'<br>';
}
}
//匿名类继承其他类中的成员
echo (new class ('andrew') extends Test_090502 {
private $name ;
public function __construct($name)
{
$this->name = $name;
}
public function hobby(){
return $this->name.'喜欢'.parent::hobby().'<br>';
}
})->hobby();
//类声明中嵌套一个匿名类
class Lakers {
private $owner ='珍妮巴斯';
public $name = '湖人';
protected $home = '洛杉矶';
public function show1(){
// 宿主类中的私有成员不能在匿名类中直接使用
// 可以通过在匿名类创建一个构造方法将宿主类中的私有成员进行注入
// 将宿主类中的私有属性做为匿名类的构造方法的参数传入即可
return (new class ($this->owner)extends Lakers {
private $owner;
public function __construct($owner){
$this->owner=$owner;
}
public function show2(){
return $this->name.'的主场在'.$this->home.'球队老板是'.$this->owner;
}
});
}
}
echo (new Lakers())->show1()->show2();点击 "运行实例" 按钮查看在线实例
Trait类的声明与工作原理;
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/9/8
* Time: 11:17
*/
class Ins{
private $name;
private $sex;
private $age;
public function __construct($age=18,$name='aa',$sex='男')
{
$this->name=$name;
$this->age=$age;
$this->sex=$sex;
}
public function instruct(){
return '我是:'.$this->name.'性别:'.$this->sex.'年龄:'.$this->age;
}
}
trait Ins_andrew{
public $name;
public $sex;
public $age;
public function instruct($name='andrew',$sex='男',$age='25'){
return '我是:'.$name.'性别:'.$sex.'年龄:'.$age;
}
}
trait Ins_wy{
public $name;
public $sex;
public $age;
public function instruct($name='wy',$sex='男',$age='25'){
return '我是:'.$name.'性别:'.$sex.'年龄:'.$age;
}
}
class Ins_ extends Ins {
use Ins_andrew,Ins_wy{
Ins_andrew::instruct insteadof Ins_wy;
Ins_wy::instruct as ins;
}
}
echo (new Ins_())->instruct();
echo (new Ins_())->ins();点击 "运行实例" 按钮查看在线实例
类的自动加载函数的写法;
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/9/8
* Time: 13:59
*/
spl_autoload_register(function ($className){
//$className就是使用的类的名字,随时绑定
require './class/'.$className.'.php';
});
echo Demo1::NAME;
echo Demo2::NAME;点击 "运行实例" 按钮查看在线实例
对象的序列化与反序列化的原理与应用
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/9/8
* Time: 15:20
*/
class Db{
private $dbname=null;
private $user ;
private $pass ;
private $host ;
//构造方法初始化
public function __construct($host='127.0.0.1',$pass='root',$user='root')
{
$this->user=$user;
$this->host=$host;
$this->pass=$pass;
$this->connect();
}
//数据库连接
private function connect(){
$this->dbname=mysqli_connect($this->host,$this->user,$this->pass);
}
//对象序列化
public function __sleep()
{
return ['host','user','pass'];
}
public function __wakeup()
{
$this->connect();
}
}
$obj = new Db();
echo serialize($obj);
$a=serialize($obj);
echo '<pre>';
var_dump(unserialize($a));点击 "运行实例" 按钮查看在线实例
面向对象编程的基本理解
面向对象编程的理解
面向对象就是先创建一个类,类有一些属性和方法;
然后创建一个类的实例来调用类的方法;
类是对象的模板,比如人就是一个;
人类有一些属性比如:姓名、性别、年龄、身高、体重等等;
人好有一些方法比如:吃饭、睡觉、学习;
姚明就是人类的一个实例,具有人类的方法{姓名、性别、年龄、身高、体重}和属性{吃饭、睡觉、学习、打篮球};
姚明的女儿会继承爸爸的属性会长的高;
姚明的女儿会继承爸爸的方法会打篮球;
接口就是已经创造好方法的外壳需要填充内容来实现方法
interface 人{
吃饭();
睡觉();
}
小明 implement 人{
吃饭(){
用刀叉吃牛排;
}
}
睡觉(){
在地上褥子在地上睡;
}
小红 implement 人{
吃饭(){
用勺子吃米饭;
}
}
睡觉(){
在炕上睡;
}
}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号