想知道淘宝的那些技术人员是怎么工作的吗?他们是怎么管理组织项目的呢?有哪些我们学习之处呢。笔者打算从点点滴滴,由浅入深,从个别到具体的全面的分析一番,目的在于为一起学习共同进步。
从便于维护之角度上讲,OOP是将来必发展。以下此类为php操作数据库的常用操作。
我们可以看出:1全面使用php6函数,2OOP思想运用的淋漓尽致,3没有一句多余语句,4命名方法科学。
<?php
class conn{
public $conn;
function __construct(){
$this->conn=mysqli_connect('localhost','root','123123','air');
mysqli_query($this->conn,'SET NAMES UTF8');
}
function __destruct(){
mysqli_close($this->conn);
}
public function delete($table,$condition="") {
if(empty($condition)) {
$this->halt('没有设置删除的条件');
return false;
}
$sql = "delete from ".$table." where 1=1 and ".$condition;
if(!$this->conn->query($sql)){
return false;
}else{
return true;
}//if
}
public function update($table,$dataArray,$condition=""){
if(!is_array($dataArray) || (count($dataArray) <=0)){
$this->halt('没有要更新的数据');
return false;
}
$value="";
while(list($key,$val)=each($dataArray)){
$value.=$key." = '".$val."',";
}
$value=substr($value,0,-1);
$sql="UPDATE ".$table." SET ".$value." WHERE 1=1 AND ".$condition;
if(!$this->conn->query($sql)){
return false;
}else{
return true;
}
}
public function insert($table,$dataArray){
$field="";
$value="";
if(!is_array($dataArray) || (count($dataArray) <=0)){
$this->halt('没有要更新的数据');
return false;
}
while (list($key,$val)=each($dataArray)){
$field.="$key,";
$value.="'$val',";
}
$field=substr($field,0,-1);
$value=substr($value,0,-1);
$sql="INSERT INTO ".$table." (".$field.") values (".$value." )";
if(!$this->conn->query($sql)){
return false;
}else{
return true;
}
}
public function getOne($sql,$resultType=MYSQL_ASSOC){
$q=$this->conn->query($sql);
$rt=$q->fetch_array($resultType);
return $rt;
}
public function getAll($sql){
$q=$this->conn->query($sql);
while($r=$q->fetch_array(MYSQLI_USE_RESULT)){
$rt[]=$r;
}
return $rt;
}
private function halt($msg='') {
$msg .= "\r\n".$this->conn->error;
die($msg);
}
public function descTable($t){//显示表结构
$rt=array();
$q=$this->conn->query("desc ".$t);
while($r=$q->fetch_array(MYSQLI_USE_RESULT)){
$rt[]=$r;
}
return $rt;
}
public function makeSql($table,$start,$style,$out){//帮助你生成sql 语句
$r=$this->getAll("select column_name from information_schema.columns where table_name='".$table."'");
$column="(";
$value="(";
$array="";
for($i=$start;$i<count($r);$i++){
$column.=$r[$i]["column_name"].",";
switch ($style) {
case 0:
$value.="'\$tmp[".$i."]',";
break;
case 1:
$value.="'\".\$_POST[\"".$r[$i]["column_name"]."\"].\"',";
break;
default:
$value.="'\".$".$r[$i]["column_name"].".\"',";
break;
}//swith
//输出数组形式
if($out){
//$dataArray[$r[$i]["column_name"]]="\$_POST[\"".$r[$i]["column_name"]."\"]";
$array.='"'.$r[$i]["column_name"].'"=>$_POST["'.$r[$i]["column_name"].'"],';
}
}
$column=substr($column,0,-1);
$column.=")";
$value=substr($value,0,-1);
$value.=")";
switch($out){
case 0:
return "INSERT INTO ".$table." ".$column." VALUES ".$value;
break;
case 1:
return $array;
break;
}//swith
}//makesql
public function showColumn($table,$start){
$r=$this->getAll("select column_name from information_schema.columns where table_name='".$table."'");
$show='<tr>';
for($i=$start;$i<count($r);$i++){
$show.='<td>\'.$r["'.$r[$i]["column_name"].'"].\'</td>';
}
$show.='</tr>';
return $show;
}//showColumn
}
?>
评论回复