Home > Backend Development > PHP Tutorial > What is elegant programming? What is elegant code?

What is elegant programming? What is elegant code?

WBOY
Release: 2016-07-25 08:48:56
Original
1051 people have browsed it
Personal understanding:
The so-called elegant programming means drinking coffee, touching the keyboard, and thinking about business;
The definition of elegant code is:
1. Use as few single and double quotation marks as possible;
2. Do not nest;
3. Full naming It is the most basic English word;
4. Like English, the code is divided into paragraphs;
5. An application code is clearly organized and organized. It is best to use tree relations and use less network relations.

Below is a method to implement a coherent operation of SQL query. Please give me your guidance!
  1. /**
  2. * DB coherent operation, sql conditional construction
  3. * From: EQPHP FrameWork
  4. * Author: art_youth
  5. * E-mail: 258122391@qq.com
  6. * Pub data: 2012-11-09
  7. */
  8. class query{
  9. public $sql='';
  10. public $option=array();
  11. static $keyword=array('select','from','where','group','having','order','limit');
  12. //初始化查询参数
  13. function __construct($table,$prefix=''){
  14. $this->option['from']=$prefix.$table;
  15. }
  16. //构造参数
  17. function __call($method,$param){
  18. if (in_array($method,self::$keyword)) {
  19. $this->option[$method]=$param[0];
  20. return $this;
  21. }
  22. }
  23. //输出查询结果
  24. function out($mode='sql',$rs_count=0,$now_page=1,$page_size=20){
  25. $this->sql='';
  26. foreach (self::$keyword as $key) {
  27. $value=($key == 'group' || $key == 'order') ? $key.' by' : $key;
  28. if ($key === 'where' && is_array($this->option['where'])) {
  29. $this->option['where']=self::condition($this->option['where']);
  30. }
  31. if (isset($this->option[$key]) && trim($this->option[$key])) {
  32. $this->sql.=' '.$value.' '.trim($this->option[$key]);
  33. }
  34. unset($this->option[$key]);
  35. }
  36. $this->sql=trim($this->sql);
  37. switch($mode){
  38. case 'rs':
  39. return db::rs($this->sql);
  40. case 'list':
  41. return db::rs_list($this->sql);
  42. case 'page':
  43. return db::page_list($this->sql,$rs_count,$now_page,$page_size);
  44. default:
  45. return $this->sql;
  46. }
  47. }
  48. //构造sql查询条件
  49. static function condition($data){
  50. //处理逻辑连接符
  51. $logic=' and ';
  52. if (isset($data['logic'])) {
  53. $logic=' '.$data['logic'].' ';
  54. unset($data['logic']);
  55. }
  56. //处理字符串(本生sql)
  57. if (isset($data['query'])) {
  58. $condition[]='('.$data['query'].')';
  59. unset($data['query']);
  60. }
  61. //处理条件数据
  62. foreach ($data as $key=>$value) {
  63. $condition[]='('.self::parse_expression($key,$value).')';
  64. }
  65. return implode($logic,$condition);
  66. }
  67. //解析表达式
  68. private static function parse_expression($key,$value){
  69. if (is_numeric($value)) return $key.'='.$value;
  70. if (is_string($value)) return $key.'="'.$value.'"';
  71. if (is_array($value)) {
  72. //基本条件查询
  73. if (preg_match('/^(eq|neq|gt|egt|lt|elt)$/i',$value[0])) {
  74. is_string($value[1]) && $value[1]='"'.$value[1].'"';
  75. $operator=array('eq'=>'=','neq'=>'<>','gt'=>'>','egt'=>'>=','lt'=>'<','elt'=>'<=',);
  76. return $key.$operator[$value[0]].$value[1];
  77. }
  78. //in范围查找
  79. if (in_array($value[0],array('in','not in'))) {
  80. is_array($value[1]) && $value[1]=implode(',',$value[1]);
  81. return $key.' '.$value[0].'('.$value[1].')';
  82. }
  83. //between区间查找
  84. if (in_array($value[0],array('between','not between'))) {
  85. $param=is_string($value[1]) ? explode(',',$value[1]) : $value[1];
  86. return $key.' '.$value[0].' '.$param[0].' and '.$param[1];
  87. }
  88. //like模糊匹配
  89. if (in_array($value[0],array('like','not like'))) {
  90. if (is_array($value[1])) {
  91. $buffer=array();
  92. foreach ($value[1] as $param) {
  93. $buffer[]=$key.' '.$value[0].' "'.$param.'"';
  94. }
  95. $logic=isset($value[2]) ? ' '.$value[2].' ' : ' or ';
  96. return implode($logic,$buffer);
  97. }
  98. if (strpos($key,'|') !== false) {
  99. $buffer=array();
  100. foreach (explode('|',$key) as $field) {
  101. $buffer[]='('.$field.' '.$value[0].' "'.$value[1].'")';
  102. }
  103. return implode(' or ',$buffer);
  104. }
  105. if (strpos($key,'&') !== false) {
  106. $buffer=array();
  107. foreach (explode('&',$key) as $field) {
  108. $buffer[]='('.$field.' '.$value[0].' "'.$value[1].'")';
  109. }
  110. return implode(' and ',$buffer);
  111. }
  112. return $key.' '.$value[0].' "'.$value[1].'"';
  113. }
  114. //数学区间查询(1,9)/[2,3)
  115. if ($value[0] === 'extent') {
  116. $logic=isset($value[2]) ? ' '.$value[2].' ' : ' && ';
  117. $operator=array('('=>'>','['=>'>=',')'=>'<',']'=>'<=');
  118. preg_match('/^((|[)(.*),(.*)()|])$/',$value[1],$param);
  119. $result='';
  120. isset($param[2]) && $result.=$key.$operator[$param[1]].$param[2];
  121. isset($param[4]) && $result.=$logic.$key.$operator[$param[4]].$param[3];
  122. return $result;
  123. }
  124. return '';
  125. }
  126. }
  127. //资源回收
  128. function __destruct(){
  129. unset($this->option,$this->sql);
  130. }
  131. }
复制代码


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template