问题
If I have a function in php that creates several arrays of objects from parsing xml, how do I return those arrays as references?
Do I need to call new to allocate the arrays? How do I define them within the function?
function ParseConfig($rawxml, &$configName, &$radioArr, &$flasherArr, &$irdArr)
Sorry, I mean return multiple arrays as parameter references.
what do I do to create the array inside the function? or can I just start using it as an array?
回答1:
I hadn't noticed that you edited your question, which changes things entirely. You have two decent options here:
Explicitly change them at the top of the function:
function foo($bar, &$baz, &$qux) { $baz = $qux = array(); }Perhaps this is a good place to introduce an object?
class parseConfig { protected $rawXml; protected $configName; protected $radioArr = array(); protected $flasherArr = array(); protected $irdArr = array(); public function __construct($raw_xml = null) { if (!is_null($raw_xml)) { $this->rawXml = $raw_xml; $this->parse(); } } public function parse($raw_xml = null) { if (!is_null($raw_xml)) { $this->rawXml = $raw_xml; } if (empty($this->rawXml)) { return null; } $this->configName = ''; $this->radioArr = $this->flasherArr = $this->irdArr = array(); // parsing happens here, may return false return true; } public function setRawXml($raw_xml) { $this->rawXml = $raw_xml; return $this; } public function getRawXml() { return $this->rawXml; } public function getRadioArr() { return $this->radioArr; } public function getFlasherArr() { return $this->flasherArr; } public function getIrdArr() { return $this->irdArr; } }
回答2:
There is no need to use references in this case. PHP uses a copy on write mechanism which also keeps track of the number of items pointing to the current value. If you return a value from a function, and assign that result to a variable, the refcount will still only be one, since the variable used in the function will be destroyed when the function returns. You can safely edit the variable containing the value returned from the function without worrying about wasting memory.
Sample test:
function getaz() {
$array = range('a','z');
echo '2: ', memory_get_usage(), "\n";
return $array;
}
echo '1: ', memory_get_usage(), "\n";
$p = getaz();
echo '3: ', memory_get_usage(), "\n";
$p[0] = '3';
echo '4: ', memory_get_usage(), "\n";
$p = array_merge($p, range('A','Z'));
echo '5: ', memory_get_usage();
Output:
1: 337304 [initial]
2: 340024 [inside function, value is created]
3: 340072 [outside function, value is assigned]
4: 340072 [element modified but remains same size]
5: 342696 [array extended]
If I change the function to return by reference, I get the following:
1: 337312 [it took 8 bytes more memory to define the function]
2: 340032 [accounting for the extra 8 bytes, no change]
3: 340080 [accounting for the extra 8 bytes, no change]
4: 340080 [accounting for the extra 8 bytes, no change]
5: 342704 [accounting for the extra 8 bytes, no change]
Hope this helps!
For some more reading on how and why this works this way, check out this shameless blog plug that explains a little bit about how PHP deals with variables and values.
回答3:
return &$array;
But it is fine to just return the $array IMHO
回答4:
Any array returned from a function will be returned by reference until you modify that array before which a copy of the array will be made:
$myArray = functionThatReturnsArray(); //$myArray is a reference to the array returned
echo $myArray[0]; //still a reference
$myArray[0] = 'someNewValue'; //from here $myArray will be a copy of the returned array
回答5:
You can specify the array type in the argument list (since PHP 5.1), if you do that you can start using it as an array right away:
function my_function( array &$arr ) {
$arr[0] = 'someValue';
}
If you don't you should make a check at the top of your function:
function my_function( &$arr ) {
if( !is_array($arr) ) $arr = array(); // or generate an error or throw exception
$arr[0] = 'someValue';
}
来源:https://stackoverflow.com/questions/7407353/how-do-i-create-multiple-php-arrays-in-a-function-for-parameters-passed-by-refer