是否有任何命名约定来执行 php 按引用传递或按值传递?

发布于 2024-11-25 23:55:37 字数 277 浏览 0 评论 0原文

我可以写一个像这样的函数:

public function removeRubbishTag(&$aString)

另外,我可以有一个像这样的函数:

public function removeRubbishTag($aString)

但是当你仔细查看参数时,很容易让其他人感到困惑。 PHP 社区是否有一些命名约定来让人们轻松判断变量是 passByReference 还是 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 others. Do the PHP community have some naming convention to let people tell easily if a variable is passByReference or value? Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如何视而不见 2024-12-02 23:55:37

不幸的是,这些领域的一致性并不是 PHP 的强项。但一般来说,你的问题的答案是:“不,没有标准约定”。

旁注:

事情变得更加复杂,因为传递引用与传递引用并不完全相同:

$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.

输出:

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

虽然这个:

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

输出:

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

而这个:

$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);

输出:

2
int(2)

$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);

输出:

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

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!"
}
墨小沫ゞ 2024-12-02 23:55:37

我还没有看到 PHP 中发送到函数的变量的命名标准。至于PHP的编码标准,可以参考PEAR编码标准。 http://pear.php.net/manual/en/standards.php

但没有提及变量命名约定。

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.

萌吟 2024-12-02 23:55:37

没有命名约定。特别是 b/c 有时,与对象一样,引用的传递是隐式的。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文