开发者

Is there any naming convention to do the php pass by reference or pass by value?

I can write a function like :

public function removeRubbishTag(&$aString)

Also, I can have a function like this:

public function removeRubbishTag($aString)

But when you look at the params carefully, it is easy to confuse other开发者_运维知识库s. Do the PHP community have some naming convention to let people tell easily if a variable is passByReference or value? Thank you.


Unfortunately, consistency in these areas is not a PHP strong suit. But generally the answer to your question is, "no, there is no standard convention".

Side note:

Things are made more complicated by the fact that pass-by-reference is not quite the same thing as pass-a-reference:

$a = array(1,2,3); 
function mutate($b){array_push($b, 1); $b = 2; echo $b;}  
mutate($a); 
var_dump($a); // notice that array is not altered here.

Outputs:

2
array(3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}

While this:

$a = array(1,2,3); 
function mutate(&$b){array_push($b, 1);}  
mutate($a); 
var_dump($a); // notice that array is altered here.

Outputs:

2
array(3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
  [3]=>
  int(1)
}

And this:

$a = array(1,2,3); 
//reassigning a reference changes the original value!
function mutate(&$b){array_push($b, 1); $b = 2; echo $b;}  
mutate($a); 
var_dump($a);

Outputs:

2
int(2)

And

$a = new stdClass(); 
$a->a = 'a';
// but... modifying an object will always result in the modification: 
function mutate($b){$b->a = "B!"; $b = 2; echo $b;}  
mutate($a); 
var_dump($a);

Outputs:

2
object(stdClass)#1 (1) {
  ["a"]=>
  string(2) "B!"
}


I haven't seen a naming standard for variables being sent into functions in PHP. As for coding standards for PHP, you can reference the PEAR coding standards. http://pear.php.net/manual/en/standards.php

There's no mention of variable naming conventions for this though.


There is no naming-convention. Especially b/c sometimes, as with objects, the pass by ref is implicit.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜