Simple understanding of object-oriented Programming in PHP _php Foundation

Source: Internet
Author: User
Tags sql injection strlen

Unlike most programming languages that can be object-oriented, PHP supports both process-oriented and object-oriented programming, and PHP developers can choose between one or the other in a process-oriented and object-oriented way, but because PHP is primarily a process-oriented programming language in the previous version of PHP5, So, most of the time, PHP developers should still choose the 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 in the Process code is a very natural means, so it can not be said that object-oriented is a better than process-oriented programming, but another programming choice, of course, here is the situation in PHP.

For the PHP process and object-oriented advantages and disadvantages, I believe that a little check on the internet will be very clear, the process development cycle is short, fast release, high efficiency, object-oriented development cycle long, low efficiency but easy to maintain, improve, expand and develop API. Obviously, it's hard to say which way would be better, and instead of arguing about which programming method is better, try to play out the advantages of two different programming methods.

Back to the object-oriented programming of PHP, it is easy to feel the advantage of using the object-oriented process, the most obvious place is that the code functions more clearly, data processing, user login, content rendering and so on each written a class, in the page only need to include these classes, instantiate objects, and then use the concise statement to apply the object on the line, This with the process of data processing, user landing, as well as content and other parts written together, the former programming ideas must be more clear and easy to understand, I believe that team development should be more inclined to 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 forms or accepting URL parameters.

In a process-oriented manner, we'll call a variety of filter strings in the statement to filter the library functions or custom functions, so that the page will appear many different filter functions and even complex regular expressions, even if the page has written enough comments is inevitable or confusing, Let's look at the object-oriented approach.

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

<?php/* 
string processing class
 * parameter $length used to determine whether the string exceeds the specified length
 * Special characters in the string used in the escape SQL statement
 * The regular limit string can only be a number
 * Determine if the string is null
 * Judge string
 
length
 
///Create String handler class
Stringfiltration {
 
  //attribute
  var $length;
 
  Method
  //Construct method
  function __construct ($the _length = NULL) {
    $this->length = $the _length;
  }
  The special character
  function realescapestring ($the _string) {return
    mysql_real_escape_string ($the _) in the string used in the escape SQL statement string);
   
  Only numeric
  function Eregnumber ($the _string) {
    if Ereg ("^[0-9]+$", $the _string) in the regular limit string can be return
      true;
    else return
      false;
   
  Determines whether the string is an 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

$string = new Stringfiltration (); 

Then, when you filter or judge a string, you invoke a defined method in the class, and some statements that invoke the method appear in the page.

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


In the example above, we can see that before the object-oriented processing string, we have to define a class and then instantiate the class in the desired page and invoke the method in the class, where it seems that the object-oriented efficiency is lower and more cumbersome than the process, but the advantages are obvious, Statements that actually handle or judge a string are written inside the class, the page on which the method is invoked does not appear with complex custom functions and complex statements such as regular expressions, the structure of the page and even the structure of the entire site are clearer, and after writing a class, you can use this class later in PHP development. In the long run, efficiency is higher. As a result, developers of PHP-oriented process programming may wish to change their thinking and try object-oriented.

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.