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