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

引用的解释

时间:2011-05-16 15:55:22  来源:站内  作者:潘春会
, $bar);
// object(stdClass)#1 (1) {
//  ["member"]=>
//  string(9) "Something"
// }
// object(stdClass)#1 (1) {
//   ["member"]=>
//   string(9) "Something"
// }

// Use the 'clone' keyword to make a separate copy of the object
$bar = clone $foo;
$bar->member = "Something";
var_dump($foo, $bar);
// object(stdClass)#1 (0) { }
// object(stdClass)#2 (1) {
//   ["member"]=>
//   string(9) "Something"
// }

?>

Anonymous (02-Sep-2009 04:07)

 

 

 

After hours of confusion and reading tons of posts I finally figured out that replacing PHP 4 style object creation, where new is assigned by reference:

$node_obj =& new someClass($somearg, $moreargs);

which in PHP 5.3.0 generates an E_STRICT message telling you that "Assigning the return value of new by reference is deprecated"

with the following, where & has been removed:

$node_obj = new someClass($somearg, $moreargs);

in some cases (at least in recursive loops while creating a tree of nodes containing child nodes) requires

unset($node_obj);

before the actual object assignment line to avoid all child nodes becoming identical.

Hope that delicate piece of information will save someone else a few hours.

wernerdegroot at nospam dot gmail (15-Jul-2009 02:56)

 

 

 

In the following case you don't have to use = & operator when returning a reference:

<?php

   
class a {
        public
$a = 1;
    }
   
    class
b {
        static
$var;
       
        public static function &
gvar($i) {
            if(!(isset(
b::$var[$i]))) {
               
b::$var[$i] = new a;
            }
           
            return
b::$var[$i];
        }
    }
   
   
$b = new b;
   
$a = b
来顶一下
返回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
推荐资讯
如何找出DHCP地址池里未使用的IP地址
如何找出DHCP地址池里
国内常用的DNS列表
国内常用的DNS列表
Linux邮件服务器软件比较
Linux邮件服务器软件比
学用纯CSS打造可折叠树状菜单
学用纯CSS打造可折叠树
相关文章
栏目更新
栏目热门