Blogger Information
Blog 35
fans 0
comment 0
visits 22334
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php基础知识(匿名对象)--2018年9月10日17:51:18
Hi的博客
Original
761 people have browsed it

由于面向过程的编程在构造系统时,重用,维护,扩展等问题逻辑过于复杂,因此面向对象由此产生.

面向对象的编程主要是把构成的问题的各个事物分解成各个对象,建立对象目的不是为了完成一个步骤,而是为了描述一个事物在解决问题的过程中经历的步骤和行为,对象作为程序的基本单位将程序和数据封装在里面,提高程序的重用性,灵活性和可扩展性.

以下是我的代码

实例

<?php
echo "<h2>匿名对象与匿名类的实现过程</h2>";
class AAA
{
    private $name ;
    public function __construct($name)
    {
        $this->name=$name;
    }
    public function __get($name)
    {
        return $this->$name;
    }
    public function T()
    {
        return $this->name;
    }
}
//匿名对象
echo(new AAA('我是测试'))->T(),"<hr>";
//匿名类
echo(new class('我是匿名'){
    private $name ;
    public function __construct($name)
    {
        $this->name=$name;
    }
    public function __get($name)
    {
        return $this->$name;
    }
    public function T($name)
    {
        return $this->name.$name;
    }
})->T('类');
//继承父级的匿名类
echo"<hr>";
echo(new class('我是继承父级的匿名类') extends AAA{
    private $name ;
    public function __construct($name)
    {
        parent::__construct($name);
        $this->name=$name;
    }
    public function __get($name)
    {
        return $this->$name;
    }
})->T();
echo"<hr>";
class BBB
{

    private $salary = "在嵌套类中获取继承的私有属性";

    public function hello()
    {
        return (new class($this->salary) extends BBB
        {
            private $salary;
            public function __construct($salary)
            {
             $this->salary=$salary;
            }
            public function demo1()
            {
                return $this->salary;
            }
        }
        );
    }

}
$test5=new BBB();
echo $test5->hello()->demo1();
echo"<hr>";
echo "<h2>trait类的声明</h2>";
//可以使用!class_exists判断类名是否已经存在
trait CCC{
    public function w(){
        return "我是trait类";
    }
}

class DDD{
    use CCC;
    public function w()
    {
        return "我是父类DDD的";
    }
}
class EEE extends DDD{
    use  CCC;
}
$DDD = new DDD();
echo $DDD->w();
echo "<hr>";
$EEE = new EEE();
echo $EEE->w();//测试表明.trait类的CCC是位于父类和子类的中间.
echo"<hr>";
echo "<h2>类的自动加载</h2>";
//spl_autoload_register(function ($className) {
//    require './class/' . $className . '.php';//
//}
//echo T1::CLASS_NAME, '<hr>';
//echo T2:CLASS_NAME, '<hr>';
echo"<hr>";
echo "<h2>对象的序列化和反序列化</h2>";
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();
echo serialize($obj),'<hr>';
$tmp = serialize($obj);
var_dump(unserialize($tmp));

运行实例 »

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


Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments