(9) Five Principles of object-oriented design

Source: Internet
Author: User
: This article mainly introduces (9) The 5 principle of object-oriented design. if you are interested in PHP tutorials, please refer to it. 1. object-oriented summary:
1. high abstraction is conducive to high summarization.
2. the code is a document.
2. example of a message book based on object-oriented thinking:

Message. php

 $ Name = $ value;} public function _ get ($ name) {if (! Isset ($ this-> $ name) {$ this-> $ name = null ;}}}

GbookModel. php

 BookPath = $ bookPath;} public function getBookPath () {return $ this-> bookPath;} public function open () {} public function close () {} public function read () {return file_get_contents ($ this-> bookPath);} // write the message public function write ($ data) {$ this-> data = self: safe ($ data) -> name. "&". self: safe ($ data)-> email. "\ r \ NSAID: \ r \ n ". self: safe ($ data)-> content; return file_put_contents ($ this-> bookPath, $ this-> data, FILE_APPEND);} public static function safe ($ data) {$ reflect = new ReflectionObject ($ data); $ props = $ reflect-> getProperties (); $ messagebox = new stdClass (); foreach ($ props as $ prop) {$ ivar = $ prop-> getName (); $ messagebox-> $ ivar = trim ($ prop-> getValue ($ data);} return $ messagebox ;} public function delete () {file_put_contents ($ this-> bookPath, 'it \'s empty now ');}}
LeaveModel. php
 GetBookPath (); $ gb-> write ($ data );}}
View. php

 Write ($ g, $ data);} public function view (gbookModel $ g) {return $ g-> read ();} public function delete (gbookModel $ g) {$ g-> delete (); echo self: view ($ g) ;}/// test $ message = new message (); $ message-> name = 'phper '; $ message-> email = 'Test @ test.com'; $ message-> content = 'Love php '; $ gb = new authorControl (); $ pen = new leaveModel (); $ book = new gbookModel (); $ book-> setBookPath('test.txt '); $ gb-> message ($ pen, $ book, $ message); echo $ gb-> view ($ book); $ gb-> delete ($ book );

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

The above section introduces (9) The Five Principles of object-oriented design, including content, and hope to help those who are interested in PHP tutorials.

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.