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,104 阅读 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日 906 阅读 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日 801 阅读 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 评论
2020-12-24 JQuery获取append后的动态元素 JQuery获取append后的动态元素 jquery 1.7之前用$('.btn').live('click', function() { alert('获取到了'); }); jquery 1.7+之后用on代替live,on()方法在被选元素及子元素上添加一个或多个事件处理程序$('body').on('click', '.btn', function() { alert('获取到了'); }); 2020年12月24日 793 阅读 0 评论
2020-12-23 Layui 多图片上传 Layui 多图片上传 1.html<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>多图片上传</title> <link rel="stylesheet" href="layui/dist/css/layui.css"> <style> .layui-upload-img{width: 92px; height: 92px; margin: 0 10px 10px 0;} </style> </head> <body> <form action="2.php" method="post" enctype="multipart/form-data"> <div class="layu... 2020年12月23日 1,022 阅读 0 评论
2020-12-21 PHP中一个|或 &与两个||或 &&与的区别 PHP中一个|或 &与两个||或 &&与的区别 逻辑运算中使用|或&的时候:先执行两边的语句,再去判断逻辑运算中使用||或&&的时候:先判断左边的表达式是否成立,需要时再判断右边的表达式 2020年12月21日 750 阅读 0 评论
2020-12-15 PHP批量清空删除文件夹、文件 PHP批量清空删除文件夹、文件 <?php //访问链接如http://localhost/qlhc.php?q=123456 if (!isset($_GET['q']) && $_GET['q'] != '123456') { header("location: //$_SERVER[HTTP_HOST]"); exit(); } // 清文件缓存 $dirs = array( realpath(dirname(__DIR__) . '/cache/index/') ); // 清理缓存 foreach ($dirs as $dir) { doRmdir($dir); echo $dir . " 清理成功!"; } /** * 清空/删除 文件夹 * @param string $dirname 文件夹路径 * @param bool $self 是否删除当前文件夹 * @return bool */ function doRmdir($dirname, $self = true) { ... 2020年12月15日 780 阅读 0 评论
2020-12-15 PHP将动态网页生成HTML纯静态网页 PHP将动态网页生成HTML纯静态网页 <?php start(array( 'index_file' => __DIR__ . '/2.php', //你首页的文件名 'cache_file' => __DIR__ . '/cache/index/', //要缓存的路径 'cache_index_file' => 'index.html', //要缓存的文件名 'expire_seconds' => 43200, //过期的秒数(60秒=1分钟) )); function start($config) { $remain_seconds = $file_timestamp = 0; if (hasCached($config, $remain_seconds, $file_timestamp)) { $html = ''; $html .= file_get_contents($config['cache_file'] . $config['cache_index_file']); die($... 2020年12月15日 818 阅读 0 评论