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

引用定位

时间:2011-05-16 15:55:57  来源:站内  作者:潘春会
($v)
  {
    
$v->d = 1225;
  }

 
$A = new yy(3);
 
var_dump($A);
 
modify($A);
 
var_dump($A);
?>

Although, in general, a formal argument declared
as $v in the function 'modify' shown above, implies
that the actual argument $A, passed when calling
the function, is not modified, this is not the
case when $A is an object instance. See what the
example code outputs when executed:

object(yy)#3 (1) {
  ["d"]=>
  int(3)
}
object(yy)#3 (1) {
  ["d"]=>
  int(1225)
}

Sergio Santana: ssantana at tlaloc dot imta dot mx (11-Aug-2005 12:30)

 

Sometimes an object's method returning a reference to itself is required. Here is a way to code it:

<?php
class MyClass {
  public
$datum;
  public
$other;
 
  function &
MyRef($d) { // the method
   
$this->datum = $d;
    return
$this; // returns the reference
 
}
}

$a = new MyClass;
$b = $a->MyRef(25); // creates the reference

echo "This is object $a: n";
print_r($a);
echo
"This is object $b: n";
print_r($b);

$b->other = 50;

echo
"This is object $a, modified" .
    
" indirectly by modifying ref $b: n";
print_r($a);
?>

This code outputs:
This is object $a:
MyClass Object
(
    [datum] => 25
    [other] =>
)
This is object $b:
MyClass Object
(
    [datum] => 25
来顶一下
返回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
推荐资讯
如何找出DHCP地址池里未使用的IP地址
如何找出DHCP地址池里
国内常用的DNS列表
国内常用的DNS列表
Linux邮件服务器软件比较
Linux邮件服务器软件比
学用纯CSS打造可折叠树状菜单
学用纯CSS打造可折叠树
相关文章
栏目更新
栏目热门