2010-03-27 48 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

房子

自行车

玻璃