2017-10-18 25 views
0

以下兩種技術似乎都可以正常工作。我只想知道哪種技術是最合適的技術。將數組變量從一個函數傳遞到後續函數的正確方法是什麼

// parameters 
$text_str = 'antique picture'; 
$error_arr = array(); 

$error_arr變量被包括在參數:從參數省略

function step_one($text_str,$error_arr) { 
global $error_arr; 
//... some code goes here ... 
$error_arr['step_one'] = true; 
} 

function step_two($text_str,$error_arr) { 
    global $error_arr; 
//... some code goes here ... 
$error_arr['step_two'] = true; 
} 

// call the two functions that have $error_arr included in the parameters 
step_one($test_str,$error_arr); 
step_two($text_str,$error_arr); 

// they output the following 
print_r outputs array('step_one' => 1) 
print_r outputs array('step_one' => 1, 'step_two' => 1) 

$error_arr變量。

function step_one($text_str) { 
global $error_arr; 
//... some code goes here ... 
$error_arr['step_one'] = true; 
} 

function step_two($text_str) { 
    global $error_arr; 
//... some code goes here ... 
$error_arr['step_two'] = true; 
} 

// call the functions that have the $error_arr variable omitted from the parameters 
step_one($text_str); 
step_two($text_str); 

// the last two functions have exactly the same output as the 
// first two functions even though the `$error_arr` is not included 
// in the parameters 

print_r outputs array('step_one' => 1) 
print_r outputs array('step_one' => 1, 'step_two' => 1) 

我在共享主機上運行PHP 7.1。我在控制面板中打開了display_errors

如果我在參數中包含$error_arr變量或者如果使用從參數中省略$error_arr變量的函數,PHP不會拋出任何錯誤消息。

+2

兩種方法都是徹底_wrong_!不要使用'global'!決不!如果必須的話,您可以將該數組作爲參考參數('&$ error_arr')移交。但更好的是使用例外。 – arkascha

+0

PHP不會拋出任何錯誤,因爲您將變量命名爲$ error_arr。如果您希望PHP發出嚴重錯誤,請使用「Exception」類(http://php.net/manual/en/class.exception.php)。你的兩個例子都是令人討厭的,我會避免使用全局變量。特別是第一個例子(在聲明變量時傳入相同的變量,ouch)。 – IncredibleHat

+0

@arkascha感謝您的信息。 (&$ error_arr)似乎正在工作。我今天早些時候嘗試過,並收到錯誤消息。我在我的職能上評論過全球,並且仍在運作。 :) – Harvey

回答

0

你可以有這樣的事情:

$error_arr = [ 
    'step_one' => step_one($text_str), 
    'step_two' => step_two($text_str), 
]; 

它不是從問題這是什麼代碼的目的明確,所以一些方法或其他可能更好。例如,如果你需要處理的幾個步驟$text_str只是在這個特殊的地方 - 你可以使用閉包代替:

$processing = [ 
    'step_one' => function($str) { /* some code */ return true }, 
    'step_two' => function($str) { /* some code */ return true }, 
]; 
$results = []; 
foreach($processing as $func) { 
    array_push($results, $func($text_str)); 
} 

如果你想這些功能來分享一些變量 - 你可以把它通過use條款:

$shared = []; 
$processing = [ 
    'step_one' => function($str) use ($shared) { /* some code */ return true }, 
    'step_two' => function($str) use ($shared) { /* some code */ return true }, 
]; 
$results = []; 
foreach($processing as $func) { 
    array_push($results, $func($text_str)); 
} 
0

這兩種技術實際上是相同的技術。

下面是一個例子:

$error_arr = ['example' => 'value']; 

$not_error_arr = ['something' => 'else']; 

function step_one($text_str, $error_arr) { 
    global $error_arr; 
    $error_arr['step_one'] = true; 
} 

step_one('foo', $not_error_arr); 

var_dump($error_arr, $not_error_arr); 

這將輸出

陣列(大小= 2) '例如'=>字符串 '值'(長度= 5) 'step_one'=>布爾真

陣列(大小= 1) '東西'=>字符串 '其他'(長度= 4)

'step_one'值爲未分配給作爲參數傳遞的數組,因爲該分配已被global $error_arr覆蓋。

所以,無論你通過什麼作爲第二個參數

function step_one($text_str,$error_arr) {... 

功能中的全局定義意味着它會被忽略。它看起來就像是在寫信給它,因爲全局範圍中的變量恰好與您作爲參數傳遞的變量相同。

相關問題