????13????????????

???????????????????????????????????????????????


1.function doSomething() {  
2.    if ($someCondition) {  
3.        if ($someOtherCondition) {  
4.            if ($yetSomeOtherCondition) {  
5.                doSomethingSpecial();  
6.            }  
7.            doSomethingElse();  
8.        }  
9.    }  
10.}
 


???????????????????????????????????????????????????????


1.function doSomething() {  
2.    if (!$someCondition) {  
3.        return false;  
4.    }  
5.    if (!$someOtherCondition) {  
6.        return false;  
7.    }  
8.    if ($yetSomeOtherCondition) {  
9.        doSomethingSpecial();  
10.    }  
11.    doSomethingElse();  
12.}
 


??????????????????δ???????????????????????????????

??????????if?????????????????????????????????ж??????????????????δ???


1.function someFunc() {  
2.   if($oneThing) {  
3.      $this->doSomething();  
4.      if($anotherThing)  
5.         $this->doSomethingElse();  
6.   }  
7.}
 


????????????£?????????????????????


1.function someFunc() {  
2.   if($oneThing) {  
3.      $this->doSomething();  
4.      $this->doAnotherThing($anotherThing);  
5.   }  
6.}  
7.private doAnotherThing($anotherThing) {  
8.   if($anotherThing)  
9.      $this->doSomethingElse();  
10.}
 


????14?????????????????????????Avoid Magic Numbers and Strings??

????????????????????????к???????????????????????????????????????????δ???


1.function someFunct() {  
2.   $this->order->set(23);  
3.   $this->order->addProduct('superComputer');  
4.   $this->shoppingList->add('superComputer');  
5.}
 


??????23??“superComputer”???????????????????


1.function someFunct() {  
2.   $orderId = 23;  
3.   $selectedProductName = 'superComputer';  
4.   $this->order->set($orderId);  
5.   $this->order->addProduct($selectedProductName);  
6.   $this->shoppingList->add($selectedProductName);  
7.}
 


???????????????????Щ??????????????????壬??????????????????????????????????????????????λ????????????????????????????????????

????15?????Built-in???麯??

???????built-in??????????foreach()

??????????


1.foreach (&$myArray as $key =>$element) {  
2.   if ($element > 5) unset ($myArray[$key]);  
3.}
 


?????????????


1.$myArray = array_filter($myArray?? function ($element) { return $element <= 5;}); 
 


????PHP??????????????鷽??????????????????????????ú????????