实例
<?php /** * 匿名类 * 1.php7.0+ * 2.类似匿名函数,没有名称的类 * 3.适用于一次性创建适用 * 4.与关键字new配套使用 */ class obj{ private $name; public function doing($name,$work){ $this->name = $name; return $name.'正在'.$work; } } //1.对象访问 $obj = new obj(); echo $obj->doing('小红','写作业'),'<br>'; //匿名对象 echo (new obj())->doing('老师','上课'),'<br>'; //匿名类 echo (new class{ private $name; public function doing($name,$work){ $this->name = $name; return $name.'正在'.$work; } })->doing('Peter先生','出差'); echo '<hr>'; //****************************************** //匿名类的应用场景 //1.匿名类中可以用构造方法 echo (new class('小明'){ private $name; function __construct($name) { $this->name = $name; } public function eat($eat) { return $this->name.'喜欢吃'.$eat; } })->eat('西瓜').'<br>'; //2.匿名类可以继承其他类的成员、 class obj1{ protected $name; function __construct($name) { $this->name = $name; } function hobby($hobby) { return $this->name ? $this->name.'喜欢打'.$hobby :$hobby; } } echo (new class('Tom') extends obj1{ private $like ='喜欢'; function hobby($hobby) { return $this->name.$this->like.$hobby; } })->hobby('篮球','喜欢'); echo '<br>'; //3.在声名类中嵌套匿名类 class obj2{ //宿主、父类 public $name = '护肤品'; protected $Fen = '防晒霜'; protected $color = '颜色'; private $money; //宿主类的私有属性不能再匿名类中直接使用,可以通过在匿名类中创建私有属性,在构造方法中以参数传入 function DO1() { return (new class('99元') extends obj2{ private $money;//接收宿主类的私有属性 function __construct($money) { $this->money = $money; } function show($c) { return $this->name.$this->Fen.$c; } function size() { return $this->Fen.$this->money; } }); } } echo (new obj2())->DO1()->show('自然色').'<br>'; echo (new obj2())->DO1()->size(); /*******************************************/ /** * trait * 1.php单继承,trait 打破了限制 * 2.trait 使代码复用 * 3.使用类的语法,和类相似,但是不能实例化 * 4.trait 相当于方法集 */ //trait 子类 父类的优先级是:子类 trait 父类 class Demo1{ protected $name; function show($name,$hobby) { $this->name = $name; return $name.'爱好'.$hobby; } } trait model{ protected $Friend='张三'; function show($name,$hobby) { $this->name = $name; return $name.$this->Friend.'都喜欢'.$hobby; } } trait model1{ function show($name,$hobby) { $this->name = $name; return $name.'正在'.$hobby; } } class Demo1_1 extends Demo1 { use model,model1{ model::show insteadof model1;//model的show方法代替model1的show 方法 model1::show as getShow;//给model 的show方法起别名 } } echo (new Demo1_1())->show('小明','打篮球').'<hr>'; echo (new Demo1_1())->getShow('小明','打篮球').'<hr>'; /**********************************************/ /** * 抽象类 * 抽象类中没有方法体,不能被实例化 * 抽象类必须继承才能使用 */ abstract class Demo2{ protected $name; function __construct($name) { $this->name = $name; } abstract function show(); } class demo2_1 extends Demo2 { private $lang; function __construct($name,$lang) { parent::__construct($name); $this->lang = $lang; } function show() { return $this->name.$this->lang; } } echo (new demo2_1('苹果','8Plus'))->show(); /*********************************************/ /** * 接口 * 类是对象的模板,对象是类的实例化 * 接口就是类的模板,所以接口是一个特殊类 *接口和抽象类一样方法没有方法体,可以有类常量必须初始化 */ interface Demo3{ const Name = '中秋佳节'; function show($value); } interface Demo3_1{ function get($k); } class model3 implements Demo3 ,Demo3_1 { function show($value) { return Demo3::Name.$value; } function get($k) { return Demo3::Name.$k; } } echo (new model3())->show('家人团聚'),'<br>'; echo (new model3())->get('赏月'); /******************************************/ /** * 对象克隆 * 默认情况下对象复制是引用传递 */ class demo4{ private $name; private $sex; private $salary; function __construct($name,$sex,$salary) { $this->name = $name; $this->sex = $sex; $this->salary = $salary; } function __get($name) { return $this->$name; } function __set($name, $value) { $this->$name = $value; } function __clone() { $this->salary = 0; } } $obj4 = new demo4('韩梅','女',6000); echo '$obj的salay:'.$obj4->salary.'<br>'; $newObj = clone $obj4; $newObj->salary = 3000; echo '$newObj的salay:'.$newObj->salary.'<br>'; echo '$obj的salay:'.$obj4->salary; /**************************************/ /** * 对象序列化 */ class Db{ public $db=null; public $host = '127.0.0.1'; public $user = 'root'; public $pwd = 'root'; public $dbname = 'stu'; function __construct() { $this->connect(); } private function connect() { $this->db = mysqli_connect($this->host,$this->user,$this->pwd,$this->dbname); } function __sleep() { return ['user','host','pwd'];//序列化的时候返回的指定属性 } function __wakeup() { // TODO: Implement __wakeup() method. } } $mysql = new db(); var_dump(serialize($mysql));//类序列化 $obj5 = serialize($mysql); echo '<pre>'; var_dump(unserialize($obj5)); /*********************************************/ /** * 自动加载spl_autoload_register() * */ spl_autoload_register(function ($className){ require $className.'.php'; }); echo (new Demo7())->show(); //一下是被加载的类 /** * 自动加载spl_autoload_register() * */ spl_autoload_register(function ($className){ require $className.'.php'; }); echo (new Demo7())->show();
运行实例 »
点击 "运行实例" 按钮查看在线实例