2022-07-21 PHP面向对象的链式调用方式 PHP面向对象的链式调用方式 实现起来也还蛮简单的,只需要在每个方法最后返回$this就可以了<?php class wc { public function __construct($who) { echo "{$who}准备去上厕所了"; } public function go() { echo "1.跑出了教室"; return $this; } public function action($ss) { echo $ss . "2.到了厕所,开始尿尿"; return $this; } public function back() { echo "3.尿尿结束,回到教室"; return $this; } } $xm = new wc("小明"); $xm->go()->... 2022年07月21日 734 阅读 0 评论
2022-06-10 PHP面向对象封装MySQL PDO(已使用预处理) PHP面向对象封装MySQL PDO(已使用预处理) Mysql.class.php<?php class Mysql { public $link; public function __construct() { $this->conn(); } /** * 连接数据库,从配置文件读取配置信息 */ public function conn() { $cfg = require 'config.php'; try { $this->link = new PDO("mysql:dbname={$cfg['databaseName']};host={$cfg['host']};charset={$cfg['charset']};port={$cfg['port']}", $cfg['name'], $cfg['password']); $this->link->setAttribute(PDO::ATTR_EMU... 2022年06月10日 841 阅读 0 评论
2021-01-12 PHP面向对象封装Redis、Memcached、Memcache实例 PHP面向对象封装Redis、Memcached、Memcache实例 Cache.class.php<?php class Mysql { public $link; public function __construct() { $this->conn(); } /** * 连接数据库,从配置文件读取配置信息 */ public function conn() { $cfg = require 'config.php'; $this->link = new mysqli($cfg['host'], $cfg['name'], $cfg['password'], $cfg['databaseName'], $cfg['port']); $this->query('set names ' . $cfg['charset']); } /** * 发送query查询 * @param string $sql sql语句 * @return mix... 2021年01月12日 1,418 阅读 0 评论
2021-01-06 PHP Smarty 模板引擎 PHP Smarty 模板引擎 1.php<?php class Mini { protected $data = array(); //把外界变量放进data数组 public function assign($k, $v) { $this->data[$k] = $v; } public function comp($temp) { $html = file_get_contents($temp); //注意 下行引用自身的data属性 $html = str_replace('{$', "<?php echo \$this->data['", $html); $html = str_replace('}', "'];?>", $html); $compfile = $temp . '.php'; file_put_contents($compfile, $html); ... 2021年01月06日 1,116 阅读 0 评论
2021-01-04 PHP面向对象封装MySQL操作函数、文件上传 PHP面向对象封装MySQL操作函数、文件上传 Mysql.class.php<?php class Mysql { public $link; public function __construct() { $this->conn(); } /** * 连接数据库,从配置文件读取配置信息 */ public function conn() { $cfg = require 'config.php'; $this->link = new mysqli($cfg['host'], $cfg['name'], $cfg['password'], $cfg['databaseName'], $cfg['port']); $this->query('set names ' . $cfg['charset']); } /** * 发送query查询 * @param string $sql sql语句 * @return m... 2021年01月04日 1,178 阅读 0 评论
2020-12-30 PHP面向对象的自动加载 PHP面向对象的自动加载 实例化某个类时,如MySQL类,需要先require('mysql.php' );如果类比较多,目录也比较多,require文件时,将会变得麻烦我们需要一个自动化的解决方法--自动加载用法:声明一个函数,并注册为"自动加载函数"当系统发现某个类不存在时,会调用此函数,我们可以在函数中加载需要的类文件<?php function myLoad($class){ require $class . '.php'; } //把myLoad注册成自动加载函数 spl_autoload_register('myLoad'); new mysql();//查看效果 2020年12月30日 1,103 阅读 0 评论
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日 958 阅读 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日 905 阅读 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日 799 阅读 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(); $stu->sub(); //gf没有被继承 echo $s... 2020年12月28日 896 阅读 0 评论