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

引用定位

时间:2011-05-16 15:55:57  来源:站内  作者:潘春会
(); // Will fail in this call.
var_dump($obj->monkeys); // Will return int(0) if it even reaches here.
?>

ksamvel at gmail dot com (11-Feb-2006 01:02)

 

One may check reference to any object by simple operator==( object). Example:

  class A {}

  $oA1 = new A();

  $roA = & $oA1;

  echo "roA and oA1 are " . ( $roA == $oA1 ? "same" : "not same") . "<br>";

  $oA2 = new A();
  $roA = & $roA2;

  echo "roA and oA1 are " . ( $roA == $oA1 ? "same" : "not same") . "<br>";

Output:

roA and oA1 are same
roA and oA1 are not same

Current technique might be useful for caching in objects when inheritance is used and only base part of extended class should be copied (analog of C++: oB = oA):

class A {
  /* Any function changing state of A should set $bChanged to true */
  public function isChanged() { return $this->bChanged; }
  private $bChanged;
  //...
}

class B extends A {
// ...
  public function set( &$roObj) {
    if( $roObj instanceof A) {
      if( $this->roAObj == $roObj &&
          $roObj->isChanged()) {
        /* Object was not changed do not need to copy A part of B */
      } else {
        /* Copy A part of B */
        $this->roAObj = &$roObj;
      }
    }
  }

  private $roAObj;
}

Sergio Santana: ssantana at tlaloc dot imta dot mx (17-Dec-2005 12:41)

 

*** WARNING about OBJECTS TRICKY REFERENCES ***
-----------------------------------------------
The use of references in the context of classes
and objects, though well defined in the documentation,
is somehow tricky, so one must be very careful when
using objects. Let's examine the following two
examples:

<?php
 
class y {
    public
$d;
  }
 
 
$A = new y;
 
$A->d = 18;
  echo
"Object $A before operation:n";
 
var_dump($A);
 
 
$B = $A; // This is not an explicit (=&) reference assignment,
           // however, $A and $B refer to the same instance
           // though they are not equivalent names
 
$C =& $A; // Explicit reference assignment, $A and $C refer to
            // the same instance and they have become equivalent
            // names of the same instance
 
 
$B->d =
来顶一下
返回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
推荐资讯
如何找出DHCP地址池里未使用的IP地址
如何找出DHCP地址池里
国内常用的DNS列表
国内常用的DNS列表
Linux邮件服务器软件比较
Linux邮件服务器软件比
学用纯CSS打造可折叠树状菜单
学用纯CSS打造可折叠树
相关文章
栏目更新
栏目热门