2012-04-11 52 views
0

我們已經譯碼的兩個驗證功能,一個是像下面(它會立刻的所有字段):

function check_fields(&$arr,&$msg,$types,$lens,$captions,$requireds) {} 

和其他功能是一樣的東西如下:

function is_valid($field=NULL,$type=0 ,$length=0,$required=true) {} 

第一函數幾條代碼行,並大幅減少代碼行(大約30-35行甚至更多),另一方面,第二個沒有引用的函數增加了代碼行(大約30-35行甚至更多)。

我們要呼籲我們要驗證各個領域的第二個功能,但第一個函數(check_fields)是反之亦然。 很久以前我在一篇文章中讀到過,從性能的角度來看,帶參考參數的函數是不好的。

現在我們不知道要使用哪個函數。從性能角度來看哪一個更好?

回答

1

嗯,我想我有我的答案我自己經過一番網絡搜索:

Do not use PHP references

+1

適合你的任何東西。如果你想遵循適當的OO,然後創建類並傳遞該類的實例而不是六個參數。對象由php5中的ref傳遞,所以你可以根據需要修改它。使用類型轉換來確保正確的類被傳遞。在我看來,你正在專注於錯誤的東西....如果你想優化你的代碼集中在數據庫調用和磁盤讀寫。 – Alex 2012-04-12 13:29:04

1

使用的解決方案,更容易使用,更容易維護。 你正在談論微型優化,這是非常無用的。因爲


使用引用您的情況下,它是簡單的解決方案,需要更少的代碼。

+0

特別是當他將代碼讀取到一個較不可讀的級別,以實現微型優化! – 2012-04-11 13:35:19

+0

這不是我的答案,我真的想知道哪一個更好,爲什麼,請不要說這樣的答案。 – ALH 2012-04-11 14:54:46

+0

查看更新的帖子。 – Alex 2012-04-11 17:49:21

0

你只要記住這一點,當你叫:

<?php 
    $a = 1 ; 
    echo "As initial, the real 'a' is..".$a."<br/>"; 
    $b = &$a ; // it's meaning $b has the same value as $a, $b = $a AND SO $a = $b (must be) 
    $b += 5; 
    echo "b is equal to: ".$b." and a equal to: ".$a."<br/>"; 
    echo " Now we back as initial, when a=1... (see the source code) <br/>"; 
    $a = 1 ; 
    echo "After we back : b is equal to: ".$b." and a equal to: ".$a."<br/>"; 
    echo "Wait, why b must follow a? ... <br/> ..what about if we change b alone? (see the source code)<br/>"; 
    $b = 23; 
    echo "After we change b alone, b equal to: ".$b." and a equal to: ".$a."<br/>"; 
    echo " WHAT ?? a ALSO CHANGED ?? a and b STICK TOGETHER?!! </br>" ; 
    echo "to 'clear this, we use 'unset' function on a (see the source code)<br/> "; 
    unset($a); 
    $b = 66; 
    $a = 1; 
    echo "Now, after unset,b is equal to: ".$b." and a equal to: ".$a."<br/>"; 
    ?> 

和輸出將是:

As initial, the real 'a' is..1 
After the Reference operator...(see the source code) 
b is equal to: 11 and a equal to: 11 
Now we back as initial... (see the source code) 
After we back : b is equal to: 1 and a equal to: 1 
Wait, why b must follow a? ... 
..what about if we change b alone? (see the source code) 
After we change b alone, b equal to: 23 and a equal to: 23 
WHAT ?? a ALSO CHANGED ?? a and b STICK TOGETHER?!! 
to 'clear this, we use 'unset' function on a (see the source code) 
Now, after unset,b is equal to: 66 and a equal to: 1 

評論:

爲什麼$a變化?這是因爲&(與號運算符)使得&作爲參考變量,因此它存儲在「臨時存儲器」中。當您初始化$b= &$a時,請參閱我的評論$b = $a$a = $b,這意味着無論您何時修改$b$a也被修改,反之亦然。 他們鏈接!這是參考運算符的簡單含義。

也許在開始的時候感覺混亂,但一旦你成爲這方面的專家,您可以處理此操作,使這樣的「開關」功能:

<?php 

function bulbswitch(&$switch) 
{ 
    $switch = !$switch;  

} 

function lightbulb($a) 
{ 
    if ($a == true) { echo 'a is On <br/>';} 
    else { echo 'a is Off <br/>';} 
} 

$a = true ; 
echo 'At the begining, a is On, then.... <br/>'; 

bulbswitch($a); 
lightbulb($a); 
bulbswitch($a); 
lightbulb($a); 
bulbswitch($a); 
lightbulb($a); 
bulbswitch($a); 
lightbulb($a); 
bulbswitch($a); 
lightbulb($a); 
bulbswitch($a); 
lightbulb($a); 
?> 

輸出將是:

At the begining, a is On, then.... 
a is Off 
a is On 
a is Off 
a is On 
a is Off 
a is On 
+0

我試圖編輯這個,但你的編輯覆蓋了我的...你必須*不*縮進普通文本,否則一切都會顯示爲代碼。 – 2016-03-21 17:21:09

+0

謝謝,但現在我解決了它..請參閱? – 2016-03-21 17:24:22

+0

那麼,一切仍然在代碼塊,所以沒有;) – 2016-03-21 17:25:20

相關問題