php中scalar是什么,PHP is_scalar 用法 手册 | 示例代码

Hi ... for newbees here, I just want to mention that reference and scalar variable aren't the same. A reference is a pointer to a scalar, just like in C or C++.

php  // simple reference to scalar

$a = 2;

$ref = & $a;

echo "$a
$ref";

?>

this should print out: "2
2".

Scalar class also exists. Look below:

php

class Object_t {

var $a;

function Object_t ()  // constructor

{

$this->a = 1;

}

}

$a = new Object_t; // we define a scalar object

$ref_a = &a;

echo "$a->a
$ref->a";

?>

again, this should echo: "1
1";

Here is another method isued in OOP to acheive on working only over reference to scalar object. Using this, you won't ever have to  ask yourself if you work on a copy of the scalar or its reference. You will only possess reference to the scalar object. If you want to duplicate the scalar object, you will have to create a function for that purpose that would read by the reference the values and assign them to another scalar of the same type... or an other type, it is as you wish at that moment.

var$a;

functionobject_t{$this->a="patate_poil";

}

}

function &get_ref($object_type)

{// here we create a scalar object in memory

// and we return it by reference to the calling

// control scope.return &new$object_type;

}$ref_object_t=get_ref(object_t);

echo"$ref_object_t->a
";?>this should echo: "patate_poit
".

The only thing that I try to demonstrate is that scalar variable ARE object in memory while a reference is usualy a variable (scalar object) that contain the address of another scalar object, which contain the informations you want by using the reference.

Good Luck!

otek is popanowel HAT hotmailZ DOT cum

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值