Home > Backend Development > PHP Tutorial > php代码如何书写才漂亮

php代码如何书写才漂亮

WBOY
Release: 2016-06-13 12:56:43
Original
876 people have browsed it

php代码怎么书写才漂亮
这是个老问题了。php5中类的出现使php的逻辑和书写都进了一个层次。但对于查询之类的php的代码还是出现过程式的“乱炖”形象。特别是php现在已经不要求var变量了,匿名函数又不完美。总言之,写这类代码还是缺少美观。
如果我写js应该是这样:

<br />
( function(w) {<br />
var a = 1,<br />
    b= 2,<br />
    fun = function() {<br />
      return a + b;<br />
   };<br />
<br />
w.onload = function(){<br />
  fun();<br />
}<br />
})(window);<br />
Copy after login

这样变量与执行代码分开容易区分一点。但如果是php则要乱得多:
<br />
$a = 1;<br />
$b = 2;<br />
function fun() {<br />
    global $a;<br />
    global $b;<br />
    return $a + $b;<br />
}<br />
foreach($_GET as $key=>$val) {<br />
  $$key = urldecode($val);<br />
}<br />
if((int)$getVal > 1) {<br />
  ...<br />
} else {<br />
  ...<br />
}<br />
$val = null;<br />
$arr = array();<br />
foreach($getArr as $val) {<br />
   if(strlen($val) > 0) {<br />
      $arr[] = $val;<br />
   }<br />
}<br />
echo fun(),implode('-',$arr);<br />
exit;<br />
Copy after login

越写下去越没法看了...
看了discuz!写的xplus,它用了很多函数和类包装。但有些判断和查询的php代码也还是过程式的。我真想知道大家平时都是怎么样写php代码的?!特别是有过写.net和jsp的码农说一下!

php 书写
------解决方案--------------------
三个字母:OOP
封装CLASS,JAVA要求完全OOP所以他的代码看起来比较美观
Related labels:
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