A simple way to understand PHP's object-oriented programming, PHP object-oriented Programming _php tutorial

Source: Internet
Author: User

A simple way to understand the object-oriented programming of PHP, PHP object-oriented programming


Unlike most object-oriented programming languages, PHP supports both process-oriented and object-oriented programming, and PHP developers can freely choose one or the other in both the process-oriented and object-oriented approaches, but since PHP is primarily a process-oriented programming language in previous versions of PHP5, So most of the time PHP developers should choose a process-oriented approach to development, in fact, kayo that even if a PHP developer does not use object-oriented, he can develop a very good PHP program, we can imagine that the Web page parsing itself is very procedural, embedded in the HTML-oriented Process-oriented code is a very natural means, so it cannot be said that Oo is a more excellent programming than the process, but another programming choice, of course, this is the case in PHP.

For the advantages and disadvantages of the process-oriented and object-oriented in PHP, I believe it will be clear to look at the web for a short period of process development, fast release, high efficiency, long object-oriented development cycle, low efficiency but easy to maintain, improve, expand and develop APIs. Obviously easy to see, we can hardly say which way will be better, instead of arguing which one is better, we should try to play the advantages of two different programming methods.

Back to the object-oriented programming of PHP, in the use of object-oriented process is still easy to feel its advantages, the most obvious place is the code more clear, data processing, user login, content rendering, such as writing a class, in the page just contain these classes, instantiate objects, and then use concise statements to apply the object on the line, This is with the process of data processing, user landing, as well as content and other parts written together, the former programming ideas are definitely more clear and easy to understand, I believe that team development should be more biased towards object-oriented programming.

Here's a simple example to illustrate the pros and cons of process-oriented and object-oriented two approaches

PHP developers often need to filter strings in order to prevent problems such as SQL injection when processing a form or accepting URL parameters.

In a process-oriented way, we call a variety of library functions or custom functions that filter strings in a statement that needs to filter strings, so that there are many different filter functions and even complex regular expressions in the page, even if enough comments are written on the page. Here's a look at the object-oriented approach.

The first is to define a simple class to process strings, to write a variety of complex string processing methods (about PHP object-oriented knowledge can be Google, this article is not separately described. )

<?php/* String processing class * parameter $length used as a judge whether the string exceeds the specified length * Escape the special characters in the string used in the SQL statement * The regular limit string can only be a number * Determine whether the string is empty * Determine the string length *//create string processing Class Stringfiltration {   //attribute  var $length;   Method  //constructor method  function __construct ($the _length = NULL) {    $this->length = $the _length;  }  Escape the special characters in the string used in the SQL statement  function realescapestring ($the _string) {    return mysql_real_escape_string ($the _ string);  }     The regular limit string can only be numeric  function Eregnumber ($the _string) {    if (ereg ("^[0-9]+$", $the _string))      return true;    else      return false;  }     Determines whether the string is empty  function strlenstring ($the _string) {    return strlen ($the _string);  }     Determine string length  function ifoverstrlenlength ($the _string) {    if (strlen ($the _string) > $this->length)      return true;    else      return false;}  }?>

Then instantiate the class in a page that needs to filter the string

Then, when filtering or judging a string, the method defined in the class is called, and some statements are called in the page.

$email = $string->realescapestring ($_post[' email '); $postId = $string->eregnumber ($id);


In the example above, we can see that before object-oriented processing of strings, we have to define a class and then instantiate the class in the desired page and invoke the methods in this class, and it seems that the object-oriented efficiency is lower and cumbersome than the process, but the advantage is also obvious, The actual processing or judgment of the string statements are written in the inside of the class, in the calling method of the page does not appear a variety of complex custom functions and complex statements such as regular expressions, the structure of the page and even the entire site is more clear, and after writing a class, PHP development in the future can use this class again, In the long run, efficiency is high. So the developers who have been working with PHP for process programming may wish to take a different approach and try object-oriented.

http://www.bkjia.com/PHPjc/1127864.html www.bkjia.com true http://www.bkjia.com/PHPjc/1127864.html techarticle A simple way to understand the object-oriented programming of PHP, PHP object-oriented programming and most of the object-oriented programming language is not the same, PHP is supporting both process-oriented and object-oriented programming side ...

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