2020-12-30 PHP面向对象中的魔术方法 PHP面向对象中的魔术方法 魔术方法:某种场景下,能够自动调用的方法如: __construct、 __destruct、__set、 __get、 __isset、__unset、__call__construct(): 构造方法,new 实例时,自动调用__destruct(): 析构方法,对象销毁时自动调用__get(属性名): 当读取对象的一个不可见属性时,自动调用,并返回值不可见: 未定义或无权访问时__set(属性名,属性值): 当对一个不可见的属性赋值时,自动调用__isset(属性名): 当用isset,或empty判断一个不可见属性时,自动调用__unset(属性名): 当unset一个不可见属性时,自动调用<?php class Human{ //构造方法,new 实例时,自动调用 public function __construct(){ echo '构造方法'; } //析构方法,对象销毁时自动调用 public function __destruct(){ echo '析... 2020年12月30日 1,078 阅读 0 评论
2020-12-29 PHP面向对象中的$this、self、parent PHP面向对象中的$this、self、parent <?php // $this 本对象 // self 本类 // parent 父类 class Single { public $rand; public static $ob; //final 方法不能被子类重写,实现单例模式 final protected function __construct() { $this->rand = mt_rand(1000, 9999); } public static function getins() { if (self::$ob == null) { self::$ob = new self(); } return self::$ob; } } var_dump(Single::getins()); class Par { public function __construct() { echo mt_rand(10000, ... 2020年12月29日 1,035 阅读 0 评论
2020-12-29 PHP面向对象中单例模式 PHP面向对象中单例模式 <?php class Single { public $rand; public static $ob; //final 方法不能被子类重写,实现单例模式 final protected function __construct() { $this->rand = mt_rand(1000, 9999); } public static function getins() { if (Single::$ob == null) { Single::$ob = new Single(); } return Single::$ob; } } var_dump(Single::getins()); var_dump(Single::getins()); 2020年12月29日 912 阅读 0 评论
2020-12-28 PHP中面向对象3种权限详解 PHP中面向对象3种权限详解 public(公有)protected(受保护)private(私有)外部YNN子类中YYN本类中YYY<?php class Human { public $money = '3000'; protected $car = 'BMW'; private $gf = 'mv'; public function par() { echo $this->money; echo $this->car; echo $this->gf; } } class Stu extends Human { public function sub() { echo $this->money; echo $this->car; echo $this->gf; } } $stu = new Stu; $stu->par(); $st... 2020年12月28日 999 阅读 0 评论