Home > Backend Development > PHP Tutorial > php object-oriented programming, php object-oriented_PHP tutorial

php object-oriented programming, php object-oriented_PHP tutorial

WBOY
Release: 2016-07-12 08:57:30
Original
1081 people have browsed it

php object-oriented programming, php object-oriented

Basic principles of object-oriented programming:

  • Single responsibility: a class that only needs to do one thing
  • Open and closed: a class should be extensible, not modifiable
  • Dependency inversion: One class should not be strongly dependent on another class. Each class corresponds to another class and is replaceable
  • Configuration: Use configuration whenever possible instead of hard coding
  • Interface-oriented programming: you only need to care about the interface, not the implementation

1. Set the attributes of the class

<span>class</span><span> ShopProduct {
    </span><span>public</span> <span>$title</span> = 'default product'<span>;
    </span><span>public</span> <span>$producterMainName</span> = 'main name'<span>;
    </span><span>public</span> <span>$producterFirstName</span> = 'first name'<span>;
    </span><span>public</span> <span>$price</span> = 0<span>;
}

</span><span>$product1</span> = <span>new</span><span> ShopProduct();

</span><span>//</span><span>设置属性</span>
<span>$product1</span>->title = "My Antonia"<span>;
</span><span>$product1</span>->producterFirstName = "Cather"<span>;
</span><span>$product1</span>->producterMainName = "Willa"<span>;
</span><span>$product1</span>->price = 5.99<span>;

</span><span>//</span><span>访问</span>
<span>echo</span> 'author: '.<span>$product1</span>->producterFirstName.' '.<span>$product1</span>->producterMainName;
Copy after login

There are many problems with setting attribute values ​​using the above method:

First: PHP allows you to set attributes dynamically, and you will not get a warning if you misspell or forget the attribute name. For example, incorrectly placing

<span>$product1</span>->producterMainName = "Willa";
Copy after login

Writing

<span>$product1</span>->producterSecondName = "Willa";
Copy after login

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

Second: The class is too loose. We do not force the setting of title, price or product name. The client code can be sure that these attributes exist, but it may or may not be the default value. Ideally, we You want to set meaningful property values ​​when instantiating a ShopProduct object.

Third: You have to do something you do frequently, such as if you need to output the author’s name in full multiple times, you have to reuse it

<span>echo</span> 'author: '.<span>$product1</span>->producterFirstName.' '.<span>$product1</span>->producterMainName;
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1108855.htmlTechArticlephp object-oriented programming, php object-oriented basic principles of object-oriented programming: Single responsibility: one class, only need to do Good thing about open closure: a class should be extensible and...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template