【www.5929.com】自定义MVC框架之工具类,字符过滤类

市肆最常用的框架是codeigniter,里面会时常应用

自定义MVC框架之工具类-模型类,mvc框架工具类模型

得了最近已经退换了5个类:

www.5929.com ,ubuntu:通过封装验证码类库一步步安装php的gd扩大

自定义MVC框架之工具类-分页类的包装

自定义MVC框架之工具类-文件上传类

自定义MVC框架之工具类-图像处理类

本条模型类援助以下作用:

>连贯操作,js叫链式操作,连贯操作的函数能够打乱顺序,最后二个函数必须是试行语句的老大函数,如select,
delete, update, add等

如 $db->table( ‘user’ )->where( ‘id=1’ )->select()

等等

->支持增加各类子类模型,命名如: UserModel,
也得以用其余方法的,跟表名称不一致的Model命名,如MyUserModel等

www.5929.com 1
1 <?php 2 3 class Model { 4 protected $dbHost; //主机 5 protected
$dbUser; //用户名 6 protected $dbPwd; //密码 7 protected $dbName;
//数据库名 8 9 protected $prefix; //表前缀 10 protected $tbName;
//表名称 11 protected $charset; //字符集 12 protected $link;
//数据库连接财富 13 protected $sql; //拼接的sql语句 14 protected
$options; //sql查询条件 15 16 17 public function __construct( $config
){ 18 $this->dbHost = $config[【www.5929.com】自定义MVC框架之工具类,字符过滤类。’db_host’]; 19 $this->dbUser =
$config[‘db_user’]; 20 $this->dbPwd = $config[‘db_pwd’]; 21
$this->dbName = $config[‘db_name’]; 22 $this->prefix =
$config[‘prefix’]; 23 $this->charset = $config[‘charset’]; 24 25
$this->link = $this->connect(); 26 27 $this->tbName =
$this->getTableName(); 28 29 $this->initOptions(); 30 } 31 32
protected function initOptions(){ 33 $sqlKey = [ ‘where’, ‘group’,
‘having’, ‘limit’, ‘order’, ‘field’, ‘table’ ]; 34 foreach ( $sqlKey as
$key => $value ) { 35 $this->options[$value] = ”; 36 if( $value
== ‘table’ ) { 37 $this->options[$value] = $this->tbName; 38 }
39 } 40 } 41 42 public function table( $tbName ) { 43 if( !empty(
$tbName ) ) { 44 $this->options[‘table’] = $tbName; 45 } 46 return
$this; 47 } 48 49 public function limit( $limit ){ 50 if( !empty( $limit
) ) { 51 if( is_string( $limit ) ) { 52 $this->options[‘limit’] =
‘limit ‘ . $limit; 53 }else if ( is_array( $limit ) ) { 54
$this->options[‘limit’] = ‘limit ‘ . join( ‘,’, $limit ); 55 } 56 }
57 return $this; 58 } 59 60 public function order( $order ){ 61 if(
!empty( $order ) ) { 62 $this->options[‘order’] = ‘order by ‘ .
$order; 63 } 64 return $this; 65 } 66 67 public function having( $having
){ 68 if( !empty( $group ) ) { 69 $this->options[‘having’] =
‘having ‘ . $having; 70 } 71 return $this; 72 } 73 74 public function
group( $group ){ 75 if( !empty( $group ) ) { 76
$this->options[‘group’] = ‘group by ‘ . $group; 77 } 78 return
$this; 79 } 80 81 public function where( $where ){ 82 if( !empty( $where
) ) { 83 $this->options[‘where’] = ‘where ‘ . $where; 84 } 85
return $this; 86 } 87 88 public function field( $fields ){ 89 if(
!empty( $fields ) ) { 90 if( is_string( $fields ) ) { 91
$this->options[‘field’] = $fields; 92 }else if( is_array( $fields
) ){ 93 $this->options[‘field’] = join( ‘,’, $fields ); 94 } 95 }
96 return $this; 97 } 98 99 public function getTableName(){ 100 if(
!empty( $this->tbName ) ) { 101 return $this->prefix .
$this->tbName; 102 } 103 $className = get_class( $this ); 104
//UserModel GoodsModel, 获取Model前边的字符串(就是表名称) 105 $tbName =
strtolower( substr( $className, 0, -5 ) ); 106 return $this->prefix .
$this->tbName; 107 } 108 109 protected function connect(){ 110 $link
= mysql_connect( $this->dbHost, $this->dbUser, $this->dbPwd );
111 if( !$link ){ 112 die( “mysql数据库连接退步:” . mysql_error() );
113 } 114 mysql_select_db( $this->dbName, $link ); 115
mysql_query( $this->charset ); 116 return $link; 117 } 118 119
public function select(){ 120 $sql = ‘SELECT %FIELD% FROM %TABLE%
%WHERE% %GROUP% %HAVING% %ORDER% %LIMIT%’; 121 $sql = str_replace( 122
[‘%FIELD%’, ‘%TABLE%’, ‘%WHERE%’, ‘%GROUP%’, ‘%HAVING%’, ‘%ORDER%’,
‘%LIMIT%’], 123 [ $this->options[‘field’],
$this->options[‘table’], $this->options[‘where’],
$this->options[‘group’], $this->options[‘having’],
$this->options[‘order’], $this->options[‘limit’] ], 124 $sql
125 ); 126 $this->sql = $sql; 127 return $this->query( $sql ); 128
} 129 130 public function __get( $key ){ 131 if( $key == ‘sql’ ) { 132
return $this->sql; 133 }else if( $key == ‘prefix’ ) { 134 return
$this->prefix; 135 } 136 return false; 137 } 138 139 public function
query( $sql ){ 140
//施行语句从前,清空原本的options保存的sql语句一时拼凑数据 141
$this->initOptions(); 142 $res = mysql_query( $sql ); 143 $data =
array(); 144 if( $res && mysql_num_rows( $res ) ) { 145 while( $row =
mysql_fetch_assoc( $res ) ){ 146 $data[] = $row; 147 } 148 } 149
return $data; 150 } 151 152 public function add( $data ){ 153 $data =
$this->parse( $data ); 154 $keys = array_keys( $data ); 155 $values
= array_values( $data ); 156 $sql = ‘INSERT INTO %TABLE%(%FIELD%)
values(%VALUES%)’; 157 $sql = str_replace( 158 [ ‘%TABLE%’, ‘%FIELD%’,
‘%VALUES%’ ], 159 [ $this->options[‘table’], join( ‘,’, $keys ),
join( ‘,’, $values ) ], 160 $sql 161 ); 162 $this->sql = $sql; 163
return $this->exec( $sql, true ); 164 } 165 166 public function
delete(){ 167 $sql = ‘DELETE FROM %TABLE% %WHERE%’; 168 $sql =
str_replace( 169 [ ‘%TABLE%’, ‘%WHERE%’ ], 170 [
$this->options[‘table’]【www.5929.com】自定义MVC框架之工具类,字符过滤类。, $this->options[‘where’] ], 171 $sql
172 ); 173 $this->sql = $sql; 174 return $this->exec( $sql ); 175
} 176 177 public function exec( $sql, $isInsert = false ){ 178
$this->initOptions(); 179 $res = mysql_query( $sql ); 180 if( $res
!== false ) { 181 if( $isInsert ) { 182 return mysql_insert_id(); 183
}else { 184 return mysql_affected_rows(); 185 } 186 } 187 return
false; 188 } 189 190 public function parse( $data ) { 191 $res = [];
192 foreach( $data as $k => $v ){ 193 if( is_string( $v ) ) { 194
$res[$k] = ‘”‘ . $v . ‘”‘; 195 } 196 } 197 return $res; 198 } 199 200
public function update( $data ) { 201 $data = $this->parse( $data );
202 $fieldValue = $this->format( $data ); 203 $sql = ‘UPDATE %TABLE%
SET %FIELD% %WHERE%’; 204 $sql = str_replace( 205 [ ‘%TABLE%’,
‘%FIELD%’, ‘%WHERE%’ ], 206 [ $this->options[‘table’],
$fieldValue, $this->options[‘where’] ], 207 $sql 208 ); 209
$this->sql = $sql; 210 return $this->exec( $sql ); 211 } 212 213
//update ghostwu_user set field = value, where …. 214 protected
function format( $data ){ 215 $res = []; 216 foreach( $data as $k
=> $v ) { 217 $res[] = $k . ‘=’ . $v; 218 } 219 return join( ‘,’,
$res ); 220 } 221 222 public function __destruct(){ 223 mysql_close(
$this->link ); 224 } 225 226 public function __call( $funcName,
$args ) { 227 $str = substr( $funcName, 0, 5 ); 228 $field = substr(
$funcName, 5 ); 229 if( $str == ‘getBy’ ) { 230 echo $args[0]; 231
return $this->where( $field . ‘=”‘ . $args[0] . ‘”‘ )->select();
232 } 233 return false; 234 } 235 } 236 $config = [ 237 ‘db_host’
=> ‘localhost’, 238 ‘db_user’ => ‘root’, 239 ‘db_pwd’ => ”,
240 ‘prefix’ => ‘ghostwu_’, 241 ‘db_name’ => ‘blog’, 242
‘charset’ => ‘utf8’, 243 ]; 244 $db = new Model( $config ); 245
print_r( $db->field( ‘*’ )->table( $db->prefix . ‘users’
)->getByName( ‘zhangsan’ ) ); 246 echo $db->sql . PHP_EOL; 247
//$db->table( $db->prefix . ‘users’ )->where( ‘id=1’
)->update( [ ‘name’ => ‘david’, ’email’ =>
‘[email protected]’
] ); 248 //echo $db->sql . PHP_EOL; 249 //$db->table(
$db->prefix . ‘users’ )->where( ‘id in( 4, 5, 6, 7 )’
)->delete(); 250 //echo $db->sql . PHP_EOL; 251 //$db->table(
$db->prefix . ‘users’ )->add( [‘name’ => ‘zhangsan’, ’email’
=>
‘[email protected]’]
); 252 /* 253 $list = $db->table( $db->prefix . ‘users’
)->field( [ ‘name’, ’email’ ] )->where( ‘id >= 1’
)->order( ‘id desc’ )->limit( [0, 5] )->select(); 254 echo
$db->sql . PHP_EOL; 255 print_r( $list ); 256 */ 257 ?> View Code

 

停止方今曾经济体改换了5个类: ubuntu:通过封装验证码类库一步步装置php的gd扩充自定义MVC框…

详见代码如下:

 代码如下

$db->select()->from()->where()

复制代码 代码如下:

class db {
    private $mysqli; //数据库连接
    private $options; //SQL选项
    private $tableName; //表名
    public function __construct($tabName) {
        $this->tableName = $tabName;
        $this->db ();
    }
    private function db() {
        $this->mysqli = new mysqli ( ‘localhost’, ‘root’, ”, ‘hdcms’
);
        $this->mysqli->query(“SET NAMES GBK”);
    }
    public function fields($fildsArr) {
        if (empty ( $fildsArr )) {
            $this->options [‘fields’] = ”;
        }
        if (is_array ( $fildsArr )) {
            $this->options [‘fields’] = implode ( ‘,’, $fildsArr
);
        } else {
            $this->options [‘fields’] = $fildsArr;
        }
        return $this;
    }
    public function order($str) {
        $this->options [‘order’] = “ORDER BY ” . $str;
        return $this;
    }
    public function select() {
        $sql = “SELECT {$this->options[‘fields’]} FROM
{$this->tableName}  {$this->options[‘order’]}”;
        return $this->query ( $sql );
    }
    private function query($sql) {
        $result = $this->mysqli
            ->query ( $sql );
        $rows = array ();
        while ( $row = $result->fetch_assoc () ) {
            $rows [] = $row;
        }
        return $rows;
    }
    private function close() {
        $this->mysqli
            ->close ();
    }
    function __destruct() {
        $this->close ();
    }
}
$chanel = new db ( “hdw_channel” );
$chanelInfo = $chanel->fields ( ‘id,cname,cpath’ )
    ->select ();
echo “<pre>”;
print_r ( $chanelInfo );

诸如此比的口舌,某个新手朋友就能够认为离奇,那是怎么落到实处的????

<?php
abstract class Filter { //filter parent class
private $blackstr = array();
private $whitestr = array();
function filtit($str) {
//do something
}
}
class LoginFilter extends Filter { //for user login filte
username(过滤注册的用户名)
function filtit($str) {
$this -> blackstr = array(
´/[\x7f-\xff]/´, //filter chinese include chinese symbol
´/\W/´ //filter all english symbol
);
return preg_replace($this->blackstr, ´´, $str);
}
}
class EditorFilter extends Filter { //for article editor
filter(过滤在线编辑器内容)
function filtit($str) {
$this -> blackstr = array(
´/\&/´,
´/\´/´,
´/\”/´,
´/\</´,
´/\>/´,
´/\\\\/´,
´/\//´,
´/-/´,
´/\*/´,
´/ /´
);
$this -> whitestr = array(
´&´,
´’´,
´”´,
´<´,
´>´,
´\´,
´/´,
´-´,
´*´,
´ ´
);
return preg_replace($this->blackstr, $this -> whitestr, $str);
}
}
class SQLFilter extends Filter { //for filte sql query
string(过滤如查询或任何sql语句)
function filtit($str) {
$this -> blackstr = array(
´/\´/´,
´/-/´
);
return preg_replace($this->blackstr, ´´, $str);
}
}
class FileNameFilter extends Filter { //for filte a file
name(过滤文件名如下载文件名)
function filtit($str) {
$this -> blackstr = array(
´/[^A-za-z0-9_\.]|\\\\|\^|\[|\]/´
);
return preg_replace($this->blackstr, ´´, $str);
}
}
?>

class a {
    protected  function aa(){
        echo 222;
    }
}
class b extends a{
    function bb(){
        $this->aa();
    }
}
$c = new b();
$c->bb();

实际上原理相比较简单,做个例证给大家看看就明白了

应用办法如:

public   公有的:本类,子类,外界对象都足以调用
protected 受珍重的:本类 子类,能够施行,外界对象不可能调用
private 私有的:只好本类施行,子类与外表对象都不得调用

class demo{
 public $sql = “”;
 function select($str)
 {
  $this->sql .= $str;
  return $this;
 }
 function from($str)
 {
  $this->sql .= $str;
  return $this;
 }
 function where($str)
 {
  $this->sql .= $str;
  return $this;
 }
 function query()
 {
  execute….
  return array
 }
}

复制代码 代码如下:

实在首要的正是,链式函数每趟回到的都以目的本身的援引,然后随即再调用本身的函数!!直到最后query函数终结!

$filter = new FileNameFilter(); //定义实例
$downFile = $filter->filtit($_GET[´fn´]); //调用过滤方法

您也许感兴趣的稿子:

  • php过滤HTML标签、属性等正则表达式汇总
  • php正则过滤html标签、空格、换行符的代码(附表达)
  • PHP过滤★等特殊符号的正则
  • PHP中过滤常用竹签的正则表明式
  • PHP正则表达式过滤html标签属性(DEMO)
  • php中过滤违规字符的求实贯彻
  • PHP中字符安全过滤函数使用小结
  • php表单敏感字符过滤类
  • php过滤全数恶意字符(批量过滤post,get敏感数据)
  • PHP使用正则表达式完成过滤违法字符串成效示例

Leave a Comment.