2010-03-27 69 views
52

我有外面的數組:給我的功能獲得外界變量

$myArr = array(); 

我想給我的函數訪問外面的陣列,它可以增加值,它

function someFuntion(){ 
    $myVal = //some processing here to determine value of $myVal 
    $myArr[] = $myVal; 
} 

我如何賦予函數正確的變量範圍?

回答

93

默認情況下,當你在一個函數中時,你不能訪問外部變量。


如果你希望你的函數能夠訪問外部變量,你必須將其聲明爲global,裏面的功能:

function someFuntion(){ 
    global $myArr; 
    $myVal = //some processing here to determine value of $myVal 
    $myArr[] = $myVal; 
} 

有關詳細信息,請參閱Variable scope

但請注意,使用全局變量不是一個好的做法:有了這個,你的函數不再是獨立的。


更好的方法是使你的函數返回結果

function someFuntion(){ 
    $myArr = array();  // At first, you have an empty array 
    $myVal = //some processing here to determine value of $myVal 
    $myArr[] = $myVal;  // Put that $myVal into the array 
    return $myArr; 
} 

而且這樣調用該函數:

$result = someFunction(); 


你的功能也可以採取參數,甚至工作的參數通過參考

function someFuntion(array & $myArr){ 
    $myVal = //some processing here to determine value of $myVal 
    $myArr[] = $myVal;  // Put that $myVal into the array 
} 

然後,這樣調用該函數:

​​

有了這個:

  • 你的函數接收的外部數組作爲參數
  • 並且可以修改它,因爲它是通過參考傳遞。
  • 這比使用全局變量更好:您的函數是一個單元,與任何外部代碼無關。


有關更多信息,請閱讀PHP手冊的Functions部分,尤其是,,,下面分幾個部分:

+1

什麼是所有的反對票? – PatrikAkerstrand 2010-03-27 22:58:42

+2

@Machine:一個很好的問題^^ *(我有,因爲編輯我的答案几次添加更多的信息;也許它是downvoted,因爲不夠完整,一開始......這可能與某事有關全球的,人們不喜歡...)* – 2010-03-27 23:00:12

+3

@Machine先生反Global @Coronatus已經決定完全可行的答案是錯誤的。而他的1,662代表讓他的權利... – 2010-03-27 23:01:00

8
$myArr = array(); 

function someFuntion(array $myArr) { 
    $myVal = //some processing here to determine value of $myVal 
    $myArr[] = $myVal; 

    return $myArr; 
} 

$myArr = someFunction($myArr); 
+6

愚蠢downvoting。當然,這是整個線程中唯一正確的答案。 – user187291 2010-03-27 23:25:39

2

實現你的目標的一種可能不是很好的方式將使用全局變量。

您可以通過將global $myArr;添加到函數的開頭來實現此目的。 但是請注意,使用全局變量在大多數情況下是一個壞主意,可能是可以避免的。

的更好的辦法是通過你的數組作爲參數傳遞給你的函數:

function someFuntion($arr){ 
    $myVal = //some processing here to determine value of $myVal 
    $arr[] = $myVal; 
    return $arr; 
} 

$myArr = someFunction($myArr); 
7
Global $myArr; 
$myArr = array(); 

function someFuntion(){ 
    global $myArr; 

    $myVal = //some processing here to determine value of $myVal 
    $myArr[] = $myVal; 
} 

事先警告,一般人從全局堅持離開,因爲它有一些缺點。

你可以試試這個

function someFuntion($myArr){ 
    $myVal = //some processing here to determine value of $myVal 
    $myArr[] = $myVal; 
    return $myArr; 
} 
$myArr = someFunction($myArr); 

這將使這樣你就不會依賴於全局。

+0

功能範圍內的全局應該足夠了,你有意添加一個到'主'範圍嗎?良好的做法? (從不使用全局變量..) – svens 2010-03-27 23:07:02

+0

函數之外的「全局」是無用的,除非整個文件包含在另一個函數中。 – andreszs 2014-10-23 18:10:09

7
$foo = 42; 
$bar = function($x = 0) use ($foo){ 
    return $x + $foo; 
}; 
var_dump($bar(10)); // int(52) 
+0

那麼優雅...好的答案! – 2017-10-03 11:46:37

0

兩個答案

1.回答提出的問題。

2.一個簡單的改變等於一個更好的方法!

答案1 - 將Vars數組傳遞給類中的__construct(),您也可以將構造留空並通過函數傳遞數組。

<?php 

// Create an Array with all needed Sub Arrays Example: 
// Example Sub Array 1 
$content_arrays["modals"]= array(); 
// Example Sub Array 2 
$content_arrays["js_custom"] = array(); 

// Create a Class 
class Array_Pushing_Example_1 { 

    // Public to access outside of class 
    public $content_arrays; 

    // Needed in the class only 
    private $push_value_1; 
    private $push_value_2; 
    private $push_value_3; 
    private $push_value_4; 
    private $values; 
    private $external_values; 

    // Primary Contents Array as Parameter in __construct 
    public function __construct($content_arrays){ 

     // Declare it 
     $this->content_arrays = $content_arrays; 

    } 

    // Push Values from in the Array using Public Function 
    public function array_push_1(){ 

     // Values 
     $this->push_values_1 = array(1,"2B or not 2B",3,"42",5); 
     $this->push_values_2 = array("a","b","c"); 

     // Loop Values and Push Values to Sub Array 
     foreach($this->push_values_1 as $this->values){ 

      $this->content_arrays["js_custom"][] = $this->values; 

     } 

     // Loop Values and Push Values to Sub Array 
     foreach($this->push_values_2 as $this->values){ 

      $this->content_arrays["modals"][] = $this->values; 

     } 

    // Return Primary Array with New Values 
    return $this->content_arrays; 

    } 

    // GET Push Values External to the Class with Public Function 
    public function array_push_2($external_values){ 

     $this->push_values_3 = $external_values["values_1"]; 
     $this->push_values_4 = $external_values["values_2"]; 

     // Loop Values and Push Values to Sub Array 
     foreach($this->push_values_3 as $this->values){ 

      $this->content_arrays["js_custom"][] = $this->values; 

     } 

     // Loop Values and Push Values to Sub Array 
     foreach($this->push_values_4 as $this->values){ 

      $this->content_arrays["modals"][] = $this->values; 

     } 

    // Return Primary Array with New Values 
    return $this->content_arrays; 

    } 

} 

// Start the Class with the Contents Array as a Parameter 
$content_arrays = new Array_Pushing_Example_1($content_arrays); 

// Push Internal Values to the Arrays 
$content_arrays->content_arrays = $content_arrays->array_push_1(); 

// Push External Values to the Arrays 
$external_values = array(); 
$external_values["values_1"] = array("car","house","bike","glass"); 
$external_values["values_2"] = array("FOO","foo"); 
$content_arrays->content_arrays = $content_arrays->array_push_2($external_values); 

// The Output 
echo "Array Custom Content Results 1"; 
echo "<br>"; 
echo "<br>"; 

echo "Modals - Count: ".count($content_arrays->content_arrays["modals"]); 
echo "<br>"; 
echo "-------------------"; 
echo "<br>"; 

// Get Modals Array Results 
foreach($content_arrays->content_arrays["modals"] as $modals){ 

    echo $modals; 
    echo "<br>"; 

} 

echo "<br>"; 
echo "JS Custom - Count: ".count($content_arrays->content_arrays["js_custom"]); 
echo "<br>"; 
echo "-------------------"; 
echo "<br>"; 

// Get JS Custom Array Results 
foreach($content_arrays->content_arrays["js_custom"] as $js_custom){ 

    echo $js_custom; 
    echo "<br>"; 


} 

echo "<br>"; 

?> 

答案2 - 然而,一個簡單的改動會把它內嵌有現代化標準。 只需在類中聲明你的數組。

<?php 

// Create a Class 
class Array_Pushing_Example_2 { 

    // Public to access outside of class 
    public $content_arrays; 

    // Needed in the class only 
    private $push_value_1; 
    private $push_value_2; 
    private $push_value_3; 
    private $push_value_4; 
    private $values; 
    private $external_values; 

    // Declare Contents Array and Sub Arrays in __construct 
    public function __construct(){ 

     // Declare them 
     $this->content_arrays["modals"] = array(); 
     $this->content_arrays["js_custom"] = array(); 

    } 

    // Push Values from in the Array using Public Function 
    public function array_push_1(){ 

     // Values 
     $this->push_values_1 = array(1,"2B or not 2B",3,"42",5); 
     $this->push_values_2 = array("a","b","c"); 

     // Loop Values and Push Values to Sub Array 
     foreach($this->push_values_1 as $this->values){ 

      $this->content_arrays["js_custom"][] = $this->values; 

     } 

     // Loop Values and Push Values to Sub Array 
     foreach($this->push_values_2 as $this->values){ 

      $this->content_arrays["modals"][] = $this->values; 

     } 

    // Return Primary Array with New Values 
    return $this->content_arrays; 

    } 

    // GET Push Values External to the Class with Public Function 
    public function array_push_2($external_values){ 

     $this->push_values_3 = $external_values["values_1"]; 
     $this->push_values_4 = $external_values["values_2"]; 

     // Loop Values and Push Values to Sub Array 
     foreach($this->push_values_3 as $this->values){ 

      $this->content_arrays["js_custom"][] = $this->values; 

     } 

     // Loop Values and Push Values to Sub Array 
     foreach($this->push_values_4 as $this->values){ 

      $this->content_arrays["modals"][] = $this->values; 

     } 

    // Return Primary Array with New Values 
    return $this->content_arrays; 

    } 

} 

// Start the Class without the Contents Array as a Parameter 
$content_arrays = new Array_Pushing_Example_2(); 

// Push Internal Values to the Arrays 
$content_arrays->content_arrays = $content_arrays->array_push_1(); 

// Push External Values to the Arrays 
$external_values = array(); 
$external_values["values_1"] = array("car","house","bike","glass"); 
$external_values["values_2"] = array("FOO","foo"); 
$content_arrays->content_arrays = $content_arrays->array_push_2($external_values); 

// The Output 
echo "Array Custom Content Results 1"; 
echo "<br>"; 
echo "<br>"; 

echo "Modals - Count: ".count($content_arrays->content_arrays["modals"]); 
echo "<br>"; 
echo "-------------------"; 
echo "<br>"; 

// Get Modals Array Results 
foreach($content_arrays->content_arrays["modals"] as $modals){ 

    echo $modals; 
    echo "<br>"; 

} 

echo "<br>"; 
echo "JS Custom - Count: ".count($content_arrays->content_arrays["js_custom"]); 
echo "<br>"; 
echo "-------------------"; 
echo "<br>"; 

// Get JS Custom Array Results 
foreach($content_arrays->content_arrays["js_custom"] as $js_custom){ 

    echo $js_custom; 
    echo "<br>"; 


} 

echo "<br>"; 

?> 

兩個選項輸出相同的信息,並允許一個函數從陣列和子陣列信息推和檢索到的代碼中的任何地方(給定數據已被按下第一)。第二個選項可以更好地控制數據的使用和保護方式。它們可以按照您的需要進行修改,但如果它們用於擴展Controller,則可以在控制器正在使用的任何類中共享它們的值。這兩種方法都不需要使用Global(s)。

輸出:

陣列自定義內容結果

情態動詞 - 計數:5

一個

b

Ç

FOO

FOO

JS定製 - 計數:9

2B或不2B

房子

自行車

玻璃