PHP Object-oriented Programming, PHP Object-oriented _php tutorial

Source: Internet
Author: User

PHP Object-oriented Programming, PHP object-oriented


The basic principles of object-oriented programming:

    • Single duty: A class, only need to do a good thing
    • Open closure: A class that should be extensible, not modifiable
    • Dependency Inversion: A class that should not be strongly dependent on another class. Each class corresponds to a second class that is replaceable
    • Configure: Use configuration as much as possible instead of hard-coded
    • Interface-oriented programming: Just care about the interface, no need to care about implementation

1. Set the properties of the class

class shopproduct {    public$title = ' default product ';      Public $producterMainName = ' main name ';      Public $producterFirstName = ' First name ';      Public $price = 0;} $product 1 New shopproduct (); // Setting Properties $product 1->title = "My Antonia"; $product 1->producterfirstname = "Cather"; $product 1->productermainname = "Willa"; $product 1->price = 5.99; // Access Echo ' Author: '. $product 1->producterfirstname. ' '. $product 1->productermainname;

There are a number of problems with setting property values in these ways:

First: PHP allows properties to be set dynamically, and does not get a warning if you misspell or forget the property name. For example, mistakenly put

$product 1->productermainname = "Willa";

Writing

$product 1->productersecondname = "Willa";

, when we export the author's name, there will be unexpected results.

Second: The class is too loose, we don't have to force the title, price, or product name, and the client code can determine that these attributes exist, but it may or may not be the default, ideally we want to instantiate the Shopproduct object when we set a meaningful property value.

Third: Have to do something often, such as the need to complete the full output of the author's name, it is necessary to reuse

Echo ' Author: '. $product 1->producterfirstname. ' '. $product 1->productermainname;

http://www.bkjia.com/PHPjc/1108855.html www.bkjia.com true http://www.bkjia.com/PHPjc/1108855.html techarticle PHP Object-oriented programming, PHP Object-oriented object-oriented programming basic principles: Single duty: A class, only need to do a good thing open closed: a class, should be extensible, and ...

  • 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.