Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7638955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:17:59+00:00 2026-05-31T08:17:59+00:00

I was going through the book named Test-Driven Development By Example By the author

  • 0

I was going through the book named “Test-Driven Development By Example” By the author Kent Beck.

I am trying to write a similar function in php but not understanding the steps.

Original function:

Test function:

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

Class function:

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

My code:

Test function:

public function setup() {
   $this->dollarFive = new Dollar(5);
}

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

Class Function:

class Dollar
{   
  public function __construct($amount) {
    $this->amount = (int) $amount;
  }

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

While executing the test case i am getting the following error.

Object of class Dollar could not be converted to int

Need some help on this. How can i fix this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T08:18:00+00:00Added an answer on May 31, 2026 at 8:18 am
    return $this->amount == $this->Object;
    

    $this->amount is an int, $this->Object isn’t an int. You tried to compare each other, thus you’ll get

    Object of class Dollar could not be converted to int

    You probably mean

    return $this->amount == $this->Object->amount;
    

    However, there is also something curious in your class

    class Dollar {
      public $amount = 0; // <-- forgotten
      public function __construct($amount) {
        $this->amount = (int) $amount;
      }
    
       public function equals(Dollar $object) {
         $this->Object = $object; // <--- ?!?
         return $this->amount == $this->Object;
       }    
    }
    

    you’ll probably want just

       public function equals(Dollar $object) {
         return $this->amount == $object->amount;
       }    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was going through the AWDR book on web development with ruby on rails
I'm going through a book of general c# development, and I've come to the
I was going through some book and I decided to write my own implementation
I have been going through the book Learning WCF trying to get my head
I am going through a book trying to understand Generics with C# and I
I'm going through the Pro Git book , trying to get my head around
I am going through symfony book and trying to do jobeet project(I use propel)..
I'm new to C# and going through an example in a book. The example
I'm going through some book which tries to teach android development. In the book,
I was going through the Programming Ruby book and I'm having problems understanding the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.