This article mainly introduces PHP based on object-oriented implementation of the message of the function, combined with examples of PHP based on object-oriented message related to write, display view, log records and other related methods encapsulation and use skills, the need for friends can refer to the next
This paper introduces the message function of PHP based on object-oriented implementation. Share to everyone for your reference, as follows:
To design a message, everything will be the message of this as the core, caught what is, according to the process to go down, that is, according to the user fill out the message, message---the flow of the show.
Now think of this problem with object-oriented thinking, in the object-oriented world, will try to do the naked eye can see and invisible, but the actual existence of the object or process abstraction. Since it is the message, then there is a message content this entity, this message entity (domain) should include the name of the message, e-mail, message content and other elements, as shown in the following code
Message entity class message{public $name;//message name public $email;//message contact public $content;/message Content Public function __set ($name, $value) { $this, $name = $value; } Public Function __get ($name) { if (!isset ($this, $name)) { $this $name = NULL;} } }
The above class is called domain, which is a real, abstract entity model. Then need a message this model, this message this model includes the basic properties and basic operation of the message, the code is as follows
Class gbookmodel{private $bookPath;//message this file private $data;//Message data Public function Setbookpath ($bookPath) {$this-> ; bookpath = $bookPath; } public Function Getbookpath () {return $this->bookpath; Public Function Open () {} public Function Close () {} public function read () {return file_get_contents ($thi S->bookpath); }//write message Public function write ($data) {$this->data = Self::safe ($data)->name. " & ". Self::safe ($data)->email." said: ". Self::safe ($data)->content. Php_eol; Return file_put_contents ($this->bookpath, $this->data,file_append); }//The safe handling of simulated data, first unpacking and packaging public static function safe ($data) {$reflect = new Reflectionobject ($data); $props = $reflect->getproperties (); $messagebox = new StdClass (); foreach ($props as $props) {$ivar = $props->getname (); $messagebox $ivar = Trim ($props->getvalue ($data)); } return $messagebox; } public Function Delete () {file_put_contents ($thIs->bookpath, "It ' s empty Now"); }}
The actual message process can be more complex, may also include a series of preparation operations and log processing, so you should define a class responsible for the logical processing of data, the code is as follows
Class leavemodel{Public function write (Gbookmodel $GB, $data) { $book = $GB->getbookpath (); $GB->write ($data); Record Log }}
Finally, through a controller, responsible for a variety of operation of the package, the controller is directly user-oriented, so including the message to view, delete, message and other functions. Can be the image of this controller is the message provided by the direct user-oriented function, encapsulated the operation details, only need to call the controller's corresponding method, the code is as follows
Class Authorcontrol{public function message (Leavemodel $l, Gbookmodel $g, message $data) { //leave a message on the message book $l->write ($g, $data); } Public Function view (Gbookmodel $g) { //view message This content return $g->read (); } Public Function Delete (Gbookmodel $g) { $g->delete (); echo Self::view ($g); }}
The test code looks like this
$message = new Message; $message->name = ' Chenqionghe '; $message->email = ' cqh@cqh.net '; $message->content = ' Chenqionghe is a handson boy. '; $GB = new Authorcontrol ();//create a message related controller $pen = new Leavemodel ();//Take out pen $book = new Gbookmodel ();//Turn out notebook $book-> Setbookpath ("E:\\chenqionghe.txt"), $GB->message ($pen, $book, $message), Echo $GB->view ($book);//$GB Delete ($book);
Does this look more complicated than the object-oriented process? It's really complicated, the amount of code is increasing, and it's hard to understand. Seems to show no merit. But have you thought about the following questions?
1. If a lot of people are responsible for perfecting this message book, the establishment of a part of the entity relationship, a person responsible for the data operation layer of code, so it is easier to division of labor?
2. What if I want to develop this message further, to record in a database, or to add paging functionality?
To implement the functionality presented in the second question above, simply add a paging method to the Gookmodel class, as shown in the code below
Public Function Readbypage () { $handle = file ($this->bookpath); $count = count ($handle); $page = isset ($_get[' page ')? Intval ($_get[' page '): 1; if ($page <1 | | $page > $count) $page = 1; $pnum = 9; $begin = ($page-1) * $PNUM; $end = ($begin + $pnum) > $count? $count: $begin + $pnum; for ($i = $begin; $i < $end; $i + +) { echo ' <strong> ', $i +1, ' </strong> ', $handle [$i], ' <br/> '; } for ($i =1; $i <ceil ($count/$pnum); $i + +) { echo ' <a href= '? page= '. $i. ' rel= ' external nofollow ' rel= ' External nofollow ">". $i. ' </a> '; }}
Then add the corresponding action to the front controller, as shown in the code below
Public Function Viewbypage (Gbookmodel $g) { return $g->readbypage ();}
The operation results are as follows
With just two simple steps, you can implement the paging functionality you need, and the existing methods don't have to be modified, just add a new method to the related class. Of course, this paging is problematic when actually clicked, because it doesn't separate the action, it's all on a page. Compared with the above ideas, you can also expand the message to MySQL database.
This program only embodies a very simple design pattern, there are many things to improve the program, each programmer in the mind has a own oo. The larger the project, the more it can embody the advantages of module partitioning and object-oriented.
The following is the complete code
<?PHP//message entity class message{public $name;//message name public $email;//message contact public $content;//message content public Function __se T ($name, $value) {$this, $name = $value; Public Function __get ($name) {if (!isset ($this, $name)) {$this $name = NULL; }}}/** * Message this model, responsible for managing the message This * $bookpath: Message This property */class gbookmodel{private $bookPath;//message this file private $data;//message Data public FU Nction Setbookpath ($bookPath) {$this->bookpath = $bookPath; } public Function Getbookpath () {return $this->bookpath; Public Function Open () {} public Function Close () {} public function read () {return file_get_contents ($thi S->bookpath); Public Function Readbypage () {$handle = file ($this->bookpath); $count = count ($handle); $page = isset ($_get[' page ')? Intval ($_get[' page '): 1; if ($page <1 | | $page > $count) $page = 1; $pnum = 9; $begin = ($page-1) * $PNUM; $end = ($begin + $pnum) > $count? $count: $begin + $pnum; For ($i = $begin; $i < $end; $i + +) {echo ' <strong> ', $i +1, ' </strong> ', $handle [$i], ' <br/> '; } for ($i =1; $i <ceil ($count/$pnum); $i + +) {echo ' <a href= '? page= '. $i. ' rel= ' external nofollow ' rel= ' extern Al nofollow ">" $i. ' </a> '; }}//write message Public function write ($data) {$this->data = Self::safe ($data)->name. " & ". Self::safe ($data)->email." said: ". Self::safe ($data)->content. Php_eol; Return file_put_contents ($this->bookpath, $this->data,file_append); }//The safe handling of simulated data, first unpacking and packaging public static function safe ($data) {$reflect = new Reflectionobject ($data); $props = $reflect->getproperties (); $messagebox = new StdClass (); foreach ($props as $props) {$ivar = $props->getname (); $messagebox $ivar = Trim ($props->getvalue ($data)); } return $messagebox; } public Function Delete () {file_put_contents ($this->bookpath, "It's empty Now"); }}class leavemodel{Public FunctioN Write (Gbookmodel $gb, $data) {$book = $GB->getbookpath (); $GB->write ($data); Log}}class authorcontrol{Public function message (Leavemodel $l, Gbookmodel $g, message $data) {//Leave a message on the message book $l ->write ($g, $data); Public Function view (Gbookmodel $g) {//view message This content return $g->read (); } Public Function Viewbypage (Gbookmodel $g) {return $g->readbypage (); Public Function Delete (Gbookmodel $g) {$g->delete (); echo Self::view ($G); }} $message = new message; $message->name = ' Chenqionghe '; $message->email = ' cqh@cqh.net '; $message->content = ' Chenqionghe is a handson boy. '; $GB = new Authorcontrol ();//create a message related controller $pen = new Leavemodel ();//Take out pen $book = new Gbookmodel ();//Turn out notebook $book-> Setbookpath ("E:\\chenqionghe.txt"), $GB->message ($pen, $book, $message)//echo $gb->view ($book); Echo $GB Viewbypage ($book);//$GB->delete ($book);