作者spp72 (爱喝水的纯水)
看板PHP
标题Re: [问题] PHP5的有支援物件set属性吗?
时间Fri Aug 3 01:02:15 2007
※ 引述《tomex (Tomex Ou)》之铭言:
: 我指的属性,不是单纯变数而己
: 而是能类似像这样:
: class c
: {
: public $Money;
: public DoubleMoney
^^^^^^^^^^^^^这个地方是不是少打了function?
: {
: set
: {
: return ($this->Money * 2);
: }
: }
: }
: $c1 = new c();
: $c1->Money = 100;
: echo $c1->DoubleMoney; // 200
你的意思是$cl->DoubleMoney可以自动呼叫set()吗?
是有一个__tostring()的内定特别函式
用法大概是这样...
class c
{
public $Money;
function __tostring()
{
$ans = $this->Money * 2;
return "$ans";
}
}
$cl = new c();
$cl->Money = 100;
echo $cl;
不过有一点要注意的是__tostring()所回传的资料型态一定要是string
所以我才用双引号把$ans包起来
其他内定的函式还有__set(),__get(),__call()
不知到这样有没有解决你的问题...
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 61.224.41.16
1F:推 tomex:_set是比较符合我的需求,但任何东西都能set,有点太随便了 08/09 13:50