php 让我的函数访问外部变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2531221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Giving my function access to outside variable
提问by brett
I have an array outside:
我外面有一个数组:
$myArr = array();
I would like to give my function access to the array outside it so it can add values to it
我想让我的函数访问它外部的数组,以便它可以向它添加值
function someFuntion(){
$myVal = //some processing here to determine value of $myVal
$myArr[] = $myVal;
}
How do I give the function the right scoping to the variable?
如何为函数赋予变量正确的作用域?
回答by Pascal MARTIN
By default, when you are inside a function, you do not have access to the outer variables.
默认情况下,当您在函数内部时,您无权访问外部变量。
If you want your function to have access to an outer variable, you have to declare it as global, inside the function :
如果您希望您的函数可以访问外部变量,则必须global在函数内部将其声明为:
function someFuntion(){
global $myArr;
$myVal = //some processing here to determine value of $myVal
$myArr[] = $myVal;
}
For more informations, see Variable scope.
有关更多信息,请参阅变量范围。
But note that using global variables is not a good practice: with this, your function is not independant anymore.
但请注意,使用全局变量不是一个好习惯:这样,您的函数就不再独立了。
A better idea would be to make your function return the result:
一个更好的主意是让你的函数返回结果:
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;
}
And call the function like this :
并像这样调用函数:
$result = someFunction();
Your function could also take parameters, and even work on a parameter passed by reference:
您的函数还可以接受参数,甚至可以处理通过引用传递的参数:
function someFuntion(array & $myArr){
$myVal = //some processing here to determine value of $myVal
$myArr[] = $myVal; // Put that $myVal into the array
}
Then, call the function like this :
然后,像这样调用函数:
$myArr = array( ... );
someFunction($myArr); // The function will receive $myArr, and modify it
With this :
有了这个 :
- Your function received the external array as a parameter
- And can modify it, as it's passed by reference.
- And it's better practice than using a global variable : your function is a unit, independant of any external code.
- 您的函数接收外部数组作为参数
- 并且可以修改它,因为它是通过引用传递的。
- 这比使用全局变量更好:您的函数是一个单元,独立于任何外部代码。
For more informations about that, you should read the Functionssection of the PHP manual, and,, especially, the following sub-sections :
有关更多信息,您应该阅读PHP 手册的函数部分,尤其是以下子部分:
回答by Maxwell s.c
$foo = 42;
$bar = function($x = 0) use ($foo){
return $x + $foo;
};
var_dump($bar(10)); // int(52)
UPDATE: there is now support for arrow functions, but i will let for someone that used it more to create the answer
更新:现在支持箭头函数,但我会让更多使用它来创建答案的人
回答by Tyler Carter
Global $myArr;
$myArr = array();
function someFuntion(){
global $myArr;
$myVal = //some processing here to determine value of $myVal
$myArr[] = $myVal;
}
Be forewarned, generally people stick away from globals as it has some downsides.
预先警告,通常人们会远离全局变量,因为它有一些缺点。
You could try this
你可以试试这个
function someFuntion($myArr){
$myVal = //some processing here to determine value of $myVal
$myArr[] = $myVal;
return $myArr;
}
$myArr = someFunction($myArr);
That would make it so you aren't relying on Globals.
这样你就不用依赖全局变量了。
回答by Amy B
$myArr = array();
function someFuntion(array $myArr) {
$myVal = //some processing here to determine value of $myVal
$myArr[] = $myVal;
return $myArr;
}
$myArr = someFunction($myArr);
回答by lamas
The one and probably not so good way of achieving your goal would using global variables.
实现目标的一种可能不太好的方法是使用全局变量。
You could achieve that by adding global $myArr;to the beginning of your function.
However note that using global variables is in most cases a bad idea and probably avoidable.
您可以通过添加global $myArr;到函数的开头来实现这一点。但是请注意,在大多数情况下使用全局变量是一个坏主意,并且可能是可以避免的。
The much better way would be passing your array as an argument to your function:
更好的方法是将数组作为参数传递给函数:
function someFuntion($arr){
$myVal = //some processing here to determine value of $myVal
$arr[] = $myVal;
return $arr;
}
$myArr = someFunction($myArr);
回答by Vector-Meister
Two Answers
两个答案
1.Answer to the asked question.
1.回答问题。
2.A simple change equals a better way!
2.简单的改变等于更好的方法!
Answer 1- Pass the Vars Array to the __construct() in a class, you could also leave the construct empty and pass the Arrays through your functions instead.
答案 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>";
?>
Answer 2- A simple change however would put it inline with modern standards. Just declare your Arrays in the Class.
答案 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>";
?>
Both options output the same information and allow a function to push and retrieve information from an Array and sub Arrays to any place in the code(Given that the data has been pushed first). The second option gives more control over how the data is used and protected. They can be used as is just modify to your needs but if they were used to extend a Controller they could share their values among any of the Classes the Controller is using. Neither method requires the use of a Global(s).
这两个选项输出相同的信息,并允许函数将信息从数组和子数组推送和检索到代码中的任何位置(假设数据已被首先推送)。第二个选项可以更好地控制数据的使用和保护方式。它们可以用作只是根据您的需要进行修改,但如果它们用于扩展控制器,则它们可以在控制器使用的任何类之间共享它们的值。这两种方法都不需要使用 Global(s)。
Output:
输出:
Array Custom Content Results
数组自定义内容结果
Modals - Count: 5
模态 - 数量:5
a
一种
b
乙
c
C
FOO
食品级
foo
富
JS Custom - Count: 9
JS 自定义 - 计数:9
1
1
2B or not 2B
2B 与否 2B
3
3
42
42
5
5
car
车
house
房子
bike
自行车
glass
玻璃

