Php Tutorial: Programming idioms in php design mode _ PHP Tutorial

Source: Internet
Author: User
Php Tutorial: Programming in php design mode. Please read the previous article refactoring, and even the most thoughtful and skilled programmers cannot foresee any nuances of a software project. The problem always appears unexpectedly. please read the previous article first.

Reconstruction

Even the most thoughtful and skilled programmers cannot foresee any nuances of a software project. The problem is always unexpected and the demand may change. The result is that the code is optimized, shared, and replaced.

Refactoring is a common method: check all your code and find out the common or similar features that can be unified and simplified in it, making your code easier to maintain and expand. Refactoring also involves exploring whether a design pattern can be applied to this specific problem-which also simplifies the solution.

Rebuild: simply rename an attribute or method, and complexity: compress an existing class. Changing your code makes it conform to one or more design patterns is another refactoring-after reading this book, you may implement it.

There is no proportion to better explain the restructuring!

Let's consider two simple classes: CartLine and Cart. CartLine records the price and quantity of each item in the shopping cart. For example, CartLine may record "four-see-red polo shirts, $19.99 each ". Cart is a container used to load one or more CartLine objects and perform related computing tasks, such as the total cost of all items in the Cart.

The following is a simple implementation of CartLine and Cart:

// PHP5
Class CartLine {
Public $ price = 0;
Public $ qty = 0;
}
Class Cart {
Protected $ lines = array ();
Public function addLine ($ line ){
$ This-> lines [] = $ line;
}
Public function calcTotal (){
$ Total = 0;
// Add totals for each line
Foreach ($ this-> lines as $ line ){
$ Total + = $ line-> price * $ line-> qty;
}
// Add sales tax
$ Total * = 1.07;
Return $ total;
}
}

The first step of refactoring must have enough tests to overwrite all your code. In this way, the modified code cannot produce different results from the original code. By the way, your test code cannot be changed unless you change the requirement (the expected result of your code) or find an error in the test instance.

The following is an example of testing CartLine and Cart. it will not change during the refactoring process.

Function TestCart (){
$ Line1 = new CartLine;
$ Line1-> price = 12; $ line1-> qty = 2;
$ Line2 = new CartLine;
$ Line2-> price = 7.5; $ line2-> qty = 3;
$ Line3 = new CartLine;
$ Line3-> price = 8.25; $ line3-> qty = 1;
$ Cart = new Cart;
$ Cart-> addLine ($ line1 );
$ Cart-> addLine ($ line2 );
$ Cart-> addLine ($ line3 );
$ This-> assertEqual (
(12*2 + 7.5*3 + 8.25) * 1.07,
$ Cart-> calcTotal ());
}

Looking at the above code, you may find some "code smells" (code smell) -- It looks weird and looks like problematic code -- it's like a candidate for refactoring. (

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.