dpb42021 2013-04-28 09:41
浏览 56
已采纳

如何用Kent Beck的书TDD中的Java代表PHP?

I am teaching myself PHP as well as TDD (Using PHP and PHPUnit). I am working through the book Test-driven Development by Example by Kent Beck.

In Chapter 3, for example, He suggests this for the equality test:

public void testEquality() {
    assertTrue(new Dollar(5).equals(new Dollar(5)));
}

In the Dollar class he rewrites the equals method as such:

public boolean equals(Object object) {
   Dollar dollar = (Dollar) object;
   return amount == dollar.amount;
}

It probably does not help that I am new to PHP, but I am not sure how to translate that into PHP.

For the first function I tried:

public function testEquality(){
    $a = new Dollar(5);
    $this->assertTrue($a->equals($b = new Dollar(5)));
}

Is this the right track? As much as I know of PHP right now objects have to be assigned to a variable, correct? Before that route I explored

$this->assertTrue(new Dollar(5)->equals(new Dollar(5))); 

threw a syntax error. Surprising as refactoring the earlier testMultiplication method with $this->assertEquals(new Dollar(10), $five->times(2)); passed, however.

As far as the equals method is concerned, that's completely foreign to me, and I just don't know where to start.

How can I correctly reconstruct the above in PHP? If I can get a few right I think I can handle the rest of the examples.

As a side question, does anyone know if this Money example has been approached in PHP and is the code out there?

  • 写回答

1条回答 默认 最新

  • matlabmann 2013-04-28 10:22
    关注

    Your code is the following

    public boolean equals(Object object) {
       Dollar dollar = (Dollar) object;
       return amount == dollar.amount;
    }
    

    And it means this (line for line)

    1. define a new method called equals that will return either true or false and expects an object of type Object (in Java that means every object) as argument
    2. convert the object of type Object to type Dollar (wiki on type conversion), now you are able to use the public methods and attributes of this object
    3. compare the attribute amount from the current context with the one that comes from dollar (which comes from object)

    You can now translate the method line by line to PHP without the need of typecasting

    public function equals($object)
    {
        return $this->amount == $object->amount;
    }
    

    It is possible to use a different method declaration:

    public function equals(Dollar $object)
    

    This way you can be sure only to compare two objects of the same type.

    The usage is similar to the Java one as well:

    $a = new Dollar(5);
    $b = new Dollar(5);
    if ($a->equals($b))
        print "TRUE";
    else
        print "FALSE";
    

    To learn more about object comparison in PHP, you better read the manual

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题