Php object design (advanced tutorial)

Source: Internet
Author: User
: This article mainly introduces php object design. For more information about PHP tutorials, see. Object-oriented and process-oriented

The core difference between object-oriented and procedural programming is Responsibility assignment.

The behavior of procedural code is the continuous invocation of a series of commands and methods, and the control code executes different responsibility codes according to different conditions. This top-down control method leads to repeated and interdependent code distributed throughout the project.

Object-oriented programming transfers responsibility from client code to specialized objects to minimize mutual dependencies.

Instance description object-oriented and process-oriented

First, in the face of such a requirement, write a file reading configuration tool.
The configuration file format is as follows:
key:value

Process-oriented approach

At this time, the requirements are clear, and the implementation is simple, and the two functions are done.


  

At this time, the code is relatively compact and easy to maintain, but life cannot always be smooth sailing. to change the requirements, you need to add a new function that supports reading and writing xml files.

In this case, we assume that if is added to determine the xml suffix file to execute the xml reading and writing methods? In this way, the read and write functions both need to determine the conditions. we need to change the two places, as shown below.

Function readParams ($ sourceFile) {$ params = []; if (preg_match ("/\. xml $/I ", $ sourceFile) {// configuration method for reading files in xml format} else {// configuration method for reading files in txt mode} return $ params ;} function writeParams ($ params, $ sourceFile) {if (preg_match ("/\. txt $/I ", $ sourceFile) {// execute the Write file configuration method for writing xml} else {// execute the Write file configuration method for writing txt }}

From the above modification, we can see that if more file formats are supported, we need to add them in the read and write methods to maintain data processing consistency.

Object-oriented approach
// Define an abstract read/write class abstract class ParamHandler {protected $ sourceFile; protected $ params = []; function _ construct ($ source) {$ this-> sourceFile = $ source;} function addParams ($ key, $ val) {$ this-> params [$ key] = $ val;} function getAllParams () {return $ this-> params;} // verify the input file type, and return the object static function getInstance ($ filename) {if (preg_match ("/\. xml $/I ", $ filename) {return new XmlParseHandler ($ filename);} return new TextParseHandler ($ filename);} abstract function write (); abstract function read () ;}// define the implementation subclass class XmlParseHandler extends ParamHandler {function write () {// write method for implementing xml} function read () {// Read method for implementing xml }}

The above introduces php object design (advanced tutorial), including some content, and hopes 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.