给我的function访问外部variables

我在外面有一个数组:

$myArr = array(); 

我想给我的函数访问外面的数组,所以它可以给它添加值

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

我该如何给函数正确的范围variables?

默认情况下,当你在一个函数中时,你不能访问外部variables。

如果你希望你的函数能够访问一个外部variables,你必须在函数内声明它为globalvariables:

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

有关更多信息,请参阅variables作用域

但是请注意, 使用全局variables不是一个好的做法 :用这个,你的函数不再是独立的。

更好的办法是让你的函数返回结果

 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; } 

并调用这样的function:

 $result = someFunction(); 

你的函数也可以带参数,甚至可以通过引用传递参数

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

然后,像这样调用函数:

 $myArr = array( ... ); someFunction($myArr); // The function will receive $myArr, and modify it 

有了这个 :

  • 你的函数收到外部数组作为参数
  • 并可以修改它,因为它通过引用传递。
  • 最好的做法是使用全局variables:函数是一个单元,与任何外部代码无关。

有关这方面的更多信息,您应该阅读PHP手册的Functions部分,特别是以下子部分:

  • 函数参数
  • 返回值
 $myArr = array(); function someFuntion(array $myArr) { $myVal = //some processing here to determine value of $myVal $myArr[] = $myVal; return $myArr; } $myArr = someFunction($myArr); 
 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); 

这将使得你不依靠全局。

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

实现你的目标的一个也许不是那么好的方式将使用全局variables。

你可以通过添加global $myArr;来达到global $myArr; 到你的function的开始。 但是请注意,使用全局variables在大多数情况下是一个坏主意,可能是可以避免的。

更好的方法是将你的数组作为parameter passing给你的函数:

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

两个答案

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 – 然而,一个简单的改变将会与现代标准保持一致。 只要在class上申报你的数组。

 <?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>"; ?> 

这两个选项都会输出相同的信息,并允许一个函数将数组和子数组中的信息推送并检索到代码中的任何位置(假定数据已被先推送)。 第二个选项可以更好地控制数据的使用和保护方式。 它们可以按照您的需要使用,但是如果它们用于扩展控制器,则可以在控制器正在使用的任何类中共享它们的值。 这两种方法都不需要使用Global(s)。

输出:

数组自定义内容结果

莫代尔 – 数:5

一个

b

C

FOO

FOO

JS自定义 – 计数:9

1

2B或不2B

3

42

汽车

自行车

玻璃