免费邮箱 |加入收藏 | 会员中心 | 我要投稿 | RSS
您当前的位置:首页 > PHP专区 > PHP技巧

引用不是什么

时间:2011-05-16 15:57:52  来源:站内  作者:潘春会
);
$c = 6;
echo
"$a - $b - $c<br>";
//$a is not exist but it was only pointer ( not real part of memory) so we have to way to get value or change it
?>
----
When we want create some "pointer of pointer" in PHP i can't do that because it's impossible in PHP. We need pointer to another pointer to change the place that the pointer refers to. In your exaple you just change value of variable in function. ( no operation of pointers )

russrobinson at tectite dot com (04-May-2010 05:22)

 

 

 

The example shown is correct for scalars.

However, in PHP5 the example does not cover objects, which behave the opposite of the way this documentation indicates.

It is my view that object references in PHP5 are exactly like pointers in C++.

This example code illustrates my point:
<?php
class Dog
{
  var
$Name;
  function
Dog($name)
  {
   
$this->Name = $name;
  }
  function
GetName()
  {
    return (
$this->Name);
  }
};

$bar = new Dog("Spot");
function
foo(&$var)
{
   
$var = new Dog("Gypsy");
}
foo($bar);
echo
"<p>".$bar->GetName();
echo
"<p>"."If that said 'Gypsy', then object references are really pointers";
?>

And the output is:

****
Gypsy

If that said 'Gypsy', then object references are really pointers
****

Therefore, corrected documentation should read (or similar):

"
Unless $bar is an object, there's no way to bind $bar in the calling scope to something else using the reference mechanism since $bar is not available in the function foo (it is represented by $var, but $var has only variable contents and not name-to-value binding in the calling symbol table).

If $bar is an object, then you can change $bar in the calling scope using the reference mechanism inside a function.
"

However, note that when I said:

"It is my view that object references in PHP5 are exactly like pointers in C++."

The "exactly like" part is misleading...

To clarify:

I don't mean PHP5 object references support pointer arithmetic or they contain memory addresses.

I mean that, in PHP5, when you pass object variables to functions they *behave* exactly like pointers to objects do in the same situation when writing in C++ (with the exception of the C++ features that just don't exist in PHP, such as pointer arithmetic).
来顶一下
返回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
推荐资讯
如何找出DHCP地址池里未使用的IP地址
如何找出DHCP地址池里
国内常用的DNS列表
国内常用的DNS列表
Linux邮件服务器软件比较
Linux邮件服务器软件比
学用纯CSS打造可折叠树状菜单
学用纯CSS打造可折叠树
相关文章
栏目更新
栏目热门