{看到你进我的网站我真的是好高兴}我是一个{在校}的{英语专业的大学生}{准确地说我是被一个错误的时间举行的一次{错误}的{考试}带到这个错误的地方的常范错误的人}|虽然为{英语专业}可是我几乎把{所有}的时间都花在了{计算机},我自认为我这一辈是靠{计算机}知识而不是{英语}知识生存的。今日{建站}一个{写写心得}{做做评论}{交交朋友}{搞搞兴趣}

关于PHP5中 构造函数遇到的问题

上一篇 / 下一篇  2007-05-23 17:35:08 / 个人分类:PHP5面向对象编程

我在学习PHP中,学到 “构造函数”的时候,因为我这里总是不太懂,所以遇到了下面的这个问题,我不知道怎么解决,不知道有没有谁可以告诉我是为什么:PHPChina 开源社区门户6eh|+En"Pi6F KG
如下所示:PHPChina 开源社区门户nU/S1g-np$F
<?phpPHPChina 开源社区门户]Q2xyZ9w"E%x7S
PHPChina 开源社区门户DP3U D&n'pLl
class UnitCounterPHPChina 开源社区门户'r,o/ku;Z!PX
{PHPChina 开源社区门户SVFf#q}
    var $units = 0;
cy7skS*S_0    var $weightPerUnit = 1.0;
@#rLV/M}x0    
!cAa `&}0bh$c0    function add($n = 1)
\/UOS2^0    {
#N E0{I5kN0        $this->units = $this->units + $n;PHPChina 开源社区门户:SPJ"o4g2X[
    }PHPChina 开源社区门户0Vc&J` v X z_E
    function totalWeight()
sYi0V(H\ef0    {PHPChina 开源社区门户Lw%p#`H
        return $this->units * $this->weightPerUnit;
(QdB4L4p(]\9L`0    }
y ?x HI0    //构造函数:设定成员变量的初始值PHPChina 开源社区门户;w2@B&nU(W
//    function construct($unitWeight = 1.0)
:]\-p1{s2_&o I"]0//    {
QI]H-rt8y u3n0//        $this->weightPerUnit = $unitWeight;
d8?&V(uV*w1uf T0//        $this->units = 0;PHPChina 开源社区门户P9EctN
//    }PHPChina 开源社区门户9{;xVQ&H3s
}
b,{"Y3uF P*D0?>PHPChina 开源社区门户 N*`B:Gb+Q
<?php
"v6k*_ dJ9v1T0$apple = new UnitCounter(1.2);
(A7gM#_5A0$apple->units = 10;
dA3h#s1l(pOK'_ k0$apple->add(2);
6PHS(j^%b1x KX0//$Weight = $apple->totalWeight();PHPChina 开源社区门户*TX z;B9Y
print "There are " . $apple->totalWeight() . " kg apples.";
.m"IuNP ]%J0?>
+d,]&v1M L!_WE.x$|0这段代码执行结果为:There are 12 kg apples.
m)J3m:DM-F3t?] k5f0但是如下代码:
0zI{%e/z)W/S0<?php
-oc"yk#w%L5qz0PHPChina 开源社区门户x|S'xj4d
class UnitCounterPHPChina 开源社区门户!D/f&q5^N]#T.U&yH5e9g
{
LzAmV1X0    var $units;
-?H#Jf1Dap1Y0    var $weightPerUnit;
y0b#J8`3D0a y0   PHPChina 开源社区门户'`;c8i1f7Gp
    function add($n = 1)
ZF q#v2\:h6j0    {PHPChina 开源社区门户z8U2V]n$G+L
        $this->units = $this->units + $n;
/}%w7z KJHHpa0    }
*`!`"AN3F%yv0    function totalWeight()PHPChina 开源社区门户&m$h,VdBZ
    {
0YT \l'E8X"i0        return $this->units * $this->weightPerUnit;PHPChina 开源社区门户R ]p$hwKXM4q-^4g
    }PHPChina 开源社区门户p!e0]?3G
    //构造函数:设定成员变量的初始值PHPChina 开源社区门户r,Ip z+H&E
    function construct($unitWeight = 1.0)
vr&ffE6R)`b0    {PHPChina 开源社区门户/_GQxo/J4}
        $this->weightPerUnit = $unitWeight;PHPChina 开源社区门户@d x/zsLhXx8o
        $this->units = 0;PHPChina 开源社区门户H4kTR~(cnt
    }PHPChina 开源社区门户nm)SF6^ G2Z
}
Vo0iQ/i9a0?>PHPChina 开源社区门户Pb l.r6~V0d
<?phpPHPChina 开源社区门户a5?S,~-r
$apple = new UnitCounter(1.2);PHPChina 开源社区门户]'K;t(B2Ak3a8nUp.@
$apple->units = 10;
sW2}h4RU,j@0$apple->add(2);
V9f5W!{s{0//$Weight = $apple->totalWeight();PHPChina 开源社区门户 Z(MTui$r#Bx ee H
print "There are " . $apple->totalWeight() . " kg apples.";PHPChina 开源社区门户x4Tr b-{}*O*X
?>PHPChina 开源社区门户Z9m` Rz:N@
的执行结果为:There are 0 kg apples.
6O!`;WiW y0-------------------------------------------------
&?)j-a1F;Ftd6Z0我是在PHP5上运行的。请问有没有谁知道是为什么啊。PHPChina 开源社区门户)kE1qfV[;n
 

TAG: php construct 问题 PHP5面向对象编程

引用 删除 忆步   /   2007-05-24 11:12:03
你的意思就是第二个例子里的construct被认为是自己做的函数对不。。
但你第二个函数并没有运行这个函数啊
Enjoytit.Info 引用 删除 ispantao   /   2007-05-23 17:52:53
我自己找到答案了。
就是
function construct($unitWeight = 1.0)
就应该为
function _ _construct($unitWeight = 1.0)
 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

我的存档

数据统计

  • 访问量: 429
  • 日志数: 4
  • 建立时间: 2007-05-19
  • 更新时间: 2007-05-31

RSS订阅

Open Toolbar