登录  /  注册
博主信息
博文 32
粉丝 0
评论 0
访问量 23215
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
匿名类,Trait,类的自动加载,序列化和反序列化——2018年9月12日14点20分
Nevermore的博客
原创
524人浏览过

实例

<?php
echo '1. 匿名类中可以使用构造方法<br>';

  echo   (new class('B')
{private $name;
public function __construct($name)
{$this->name = $name;}
public function show($var1){return $this->name.'X'.$var1;}
})->show('man');


echo '<hr>';
echo '2. 在匿名类中可以继承其它类中的成员<br>';
class  Kongfu
{
    protected  $skill;
    public function __construct($skill='')
    {
        $this->skill=$skill;
    }
    public  function  show()
    {
       return  $this->skill?:'看破斩';
    }
}
 echo   (new class('阿三','自爆') extends Kongfu
{
    private $name;
    public function __construct($name,$skill = '')
    {
        parent::__construct($skill);
        $this->name=$name;
    }
    public function show()
    {
        return  $this->name.'的技能是'. parent::show();
    }
})->show();

echo '<hr>';
echo '3.可以在类声明中嵌套一个匿名类<br>';
class Animal
{
    public $name='谁?';
    protected $color='黑';
    private $type='中华小当家';
    public function demo1 ()
    {

        return  (new class($this->type) extends Animal
        {   private $type;
            public function __construct($name)
            {
                $this->type=$name;
            }

            public  function  demo2()
            {
                return '我是嵌套方法';
            }
            public function show($name)
            {
                return $this->$name;
            }
        }) ;
    }
}
//echo (new Animal())->demo1()->demo2();
echo  (new Animal())->demo1()->show('type');

运行实例 »

点击 "运行实例" 按钮查看在线实例

实例

<?php
class Person
{
    protected $name='王小明';
    public  function study($skill='php')
    {
        return $this->name.'在学习'.$skill;
    }
}

Trait Another
{
    public  function study($skill='Python')
    {
        return $this->name.'在学习'.$skill;
    }
}

Trait TheOther
{
    public  function study($skill='java')
    {
        return $this->name.'在学习'.$skill;
    }
}

class Allin extends Person
{
//    use TheOther;
//    use Another;
     use TheOther ,Another{
         Another::study insteadof TheOther;
         TheOther::study as my_study;
         }
}

$person=new Allin();
echo  $person->my_study();

运行实例 »

点击 "运行实例" 按钮查看在线实例

实例

<?php
class Mysqli1
{
    private  $db;
    private  $host;
    private  $user;
    private  $password;
    public function __construct($host,$user,$password)
    {
        $this->host=$host;
        $this->user=$user;
        $this->password=$password;
        $this->connect();

    }
    public function connect()
    {
        $this->db=mysqli_connect($this->host,$this->user,$this->password);
    }

    public  function  __sleep()
    {
        return ['host','user','password'];
    }

    public function __wakeup()
    {
        $this->connect();
    }
}
$obj= new  Mysqli1('localhost','root','');
$tmp=serialize($obj);
var_dump( unserialize($tmp));

运行实例 »

点击 "运行实例" 按钮查看在线实例

1. 匿名类中可以使用构造方法

2. 在匿名类中可以继承其它类中的成员

3.可以在类声明中嵌套一个匿名类  (1. 宿主类中的私有成员不能在匿名类中直接使用2. 可以通过在匿名类创建一个构造方法将宿主类中的私有成员进行注入3. 将宿主类中的私有属性做为匿名类的构造方法的参数传入即可)



trait 类位于 Person 与 Student之间,同名方法将父类方法重写

在2个trait 中同名方法用关键字改写 Another::study insteadof TheOther;TheOther::study as my_study;


spl_autoload_register(function($classname)

{

 require'./class/'.$classname.'.php';

});类的自动加载

存在命名空间的情况下

  $className = str_replace("\\","/", $className);     (将命名空间的分隔符替换成目录分隔符)

require './class/'.$className.'.php';



  对象序列化的特点:

 1. 只保存对象中的属性,不保存方法

 2. 只保存类名,不保存对象名


批改状态:未批改

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学