开发者

Referencing in PHP

开发者 https://www.devze.com 2023-04-05 01:14 出处:网络
Looking at referencing in PHP is pretty much confusing me, can anyone explain to me how this would work:

Looking at referencing in PHP is pretty much confusing me, can anyone explain to me how this would work:

private $TestArray1 = Array()
private $TestArray2 = Array()

private function test1(){
$this->test2($this->TestArray1);
$this->test2($this->TestArray2);
}
开发者_如何学编程
private function test2($Array){
$this->test3($Array);
}

private function test3($Array){
$Array[0] = 1;
}

Where would I be putting the "&" in this, if I wanted to have the private variables TestArray1 and TestArray2 be edited, after it is set in function test3?


private $TestArray1 = Array()
private $TestArray2 = Array()

private function test1(){
$this->test2($this->TestArray1);
$this->test2($this->TestArray2);
}

private function test2(&$Array){
$this->test3($Array);
}

private function test3(&$Array){
$Array[0] = 1;
}

So you need to specify & in the function declaration, and pass parameter as-is.


The default behavior in php 5.3 is to pass objects by reference. You can use the & symbol to force primitive types to be passed by reference too, though you normally won't have any reason to do this.

Your example is potentially confusing, as you don't seem to be inside of a class, so the private modifiers don't make sense. No reason to pass a member variable into a method, as the method could access it directly itself.

Hope that helps.

EDIT: And I also should point out that apparently arrays don't count as object for the "default pass by reference." That is, the & is required here: codepad.viper-7.com/LBcr4m

0

精彩评论

暂无评论...
验证码 换一张
取 消