PHP Design Pattern Introduction Value Object Mode 1th 5 page _php Tutorial

Source: Internet
Author: User
For example, an object is usually used to describe a date, a number, or a currency. The class definitions for dates, integers, or dollars are easy to use, fast, easy to encapsulate, and easy to copy, compare, or even create.

On the surface, these simple-to-describe objects are easy to execute: They have very few statements and are no different when constructing classes, whether they are applied to a customer or a SKU. The idea seems to be correct, but the so-called "seem right" is prone to some bugs.

Take a look at the following code, which is a definition and execution of an object that pays employees in dollars. In most cases, it runs without problems. (This class is named Baddollar because it still has a bug). Think about it and see if you can find the bug.

PHP5
Class Baddollar {
protected $amount;
Public function __construct ($amount =0) {
$this->amount = (float) $amount;
}
Public Function Getamount () {
return $this->amount;
}
Public function Add ($dollar) {
$this->amount + = $dollar->getamount ();
}
}
Class Work {
Protected $salary;p ublic function __construct () {
$this->salary = new Baddollar (200);}
Public Function PayDay () {
return $this->salary;
}
}
Class Person {
Public $wallet;
}
function testbaddollarworking () {
$job = new Work;
$p 1 = new person;
$p 2 = new person;
$p 1->wallet = $job->payday ();
$this->assertequal (1->wallet->getamount, $p ());
$p 2->wallet = $job->payday ();
$this->assertequal (2->wallet->getamount, $p ());
$p 1->wallet->add ($job->payday ());
$this->assertequal ($p 1->wallet->getamount ());
This is bad-actually 400
$this->assertequal (2->wallet->getamount, $p ());
This is really bad-actually 400
$this->assertequal ($job->payday ()->getamount ());
}
So, what is the bug? If you don't see the problem visually in the code example above, here's a hint: the Employee object $p1 and object $p2 use the same Baddollar object instance.

First, instances of class work and class person have been created. So, assuming that each employee initially had an empty e-purse, the employee's wallet Person:wallet was assigned to the object resource variable returned by the work::p Ayday () function, so it was set to an object instance of the Baddollar class.

Remember how the object assignment was handled by PHP5? Because of the way PHP5 objects are assigned, $job::salary,, $p 1::wallet, and $p2::wallet, the three seemingly different object instances use different "identifiers," but in fact, all of them are assigned to the same object instance.

Therefore, the next pay-out operation (PayDay said the date of pay, here is the action to pay wages), using $job->payday () would have just wanted to increase $p1 wages, but unexpectedly to $P2 also issued. And the move also changed the amount of basic pay for the job. As a result, the last two values are detected with an error.

Value Object PHP5 Unit Test
1) Equal expectation fails because [integer:200] differs from [float:400] by 200
In testbaddollarworking
In Valueobjtestcase
2) Equal expectation fails because [integer:200] differs from [float:400] by 200
In testbaddollarworking
In Valueobjtestcase
Failures!!!

http://www.bkjia.com/PHPjc/318876.html www.bkjia.com true http://www.bkjia.com/PHPjc/318876.html techarticle For example, an object is usually used to describe a date, a number, or a currency. The class definitions for dates, integers, or dollars are easy to use, fast, easy to encapsulate, and easy to enter ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.