Home >Backend Development >PHP Tutorial >Simple understanding of PHP's object-oriented programming method, php object-oriented programming_PHP tutorial

Simple understanding of PHP's object-oriented programming method, php object-oriented programming_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:52:16926browse

A simple understanding of PHP’s object-oriented programming. PHP object-oriented programming

Unlike most object-oriented programming languages, PHP supports both process-oriented and object-oriented programming. Programming method, PHP developers can freely choose one or a mixture of process-oriented and object-oriented. However, in versions before PHP5, PHP was mainly a process-oriented programming language, so most of the time PHP developers should It is better to choose a process-oriented approach for development. In fact, Kayo believes that even if a PHP developer does not use object-oriented at all, he can develop excellent PHP programs. We can imagine that the parsing of Web pages itself is very process-oriented. Embedding process-oriented code in HTML is a very natural method. Therefore, it cannot be said that object-oriented is a better programming method than process-oriented, it is just another programming choice. Of course, this is the case in PHP.

As for the advantages and disadvantages of process-oriented and object-oriented in PHP, I believe you will be very clear after a little research on the Internet. Process-oriented development cycle is short, release is fast, and efficiency is higher, while object-oriented development cycle is long and efficiency is lower. But easy to maintain, improve, extend and develop API. Obviously, it is difficult to say which method is better. Instead of arguing about which programming method is better, it is better to try to give full play to the advantages of both programming methods.

Back to the object-oriented programming of PHP, it is still easy to feel its advantages in the process of using object-oriented. The most obvious place is that the code function is clearer, data processing, user login, content presentation, etc. are written in one Classes, you only need to include these classes and instantiate objects in the page, and then use concise statements to apply the objects. Compared with the process-oriented process where data processing, user login, and content are written together, the former The programming ideas are definitely clearer and easier to understand. I believe that team development should be more biased toward object-oriented programming.

The following is a simple example to illustrate the advantages and disadvantages of process-oriented and object-oriented approaches

When processing forms or accepting url parameters, in order to prevent problems such as SQL injection, PHP developers often need to filter strings.

In the process-oriented approach, we will call various library functions or custom functions for filtering strings in the statements that need to filter strings. In this way, there will be many different filtering functions and even Complex regular expressions will inevitably be confusing even if enough comments are written on the page. Let's take a look at the object-oriented processing method.

First, a simple class for processing strings is defined, and various complex string processing are written as methods (you can Google for PHP object-oriented knowledge, and this article will not describe it separately.)

<&#63;php 
/* 字符串处理类
 * 参数$length用作判断字符串是否超过指定长度
 * 转义 SQL 语句中使用的字符串中的特殊字符
 * 正则限制字符串内只能为数字
 * 判断字符串是否为空
 * 判断字符串长度
 
*/
 
// 创建字符串处理类
class StringFiltration {
 
  // 属性
  var $length;
 
  // 方法
  // 构造方法
  function __construct($the_length = NULL){
    $this->length = $the_length;
  }
  // 转义 SQL 语句中使用的字符串中的特殊字符
  function realEscapeString($the_string){
    return mysql_real_escape_string($the_string);
  }
   
  // 正则限制字符串内只能为数字
  function eregNumber($the_string){
    if( ereg("^[0-9]+$",$the_string) )
      return true;
    else
      return false;
  }
   
  // 判断字符串是否为空
  function strlenString($the_string){
    return strlen($the_string);
  }
   
  // 判断字符串长度
  function ifOverStrlenLength($the_string){
    if( strlen($the_string) > $this->length )
      return true;
    else
      return false;
  }
 
}
 
&#63;>

Then instantiate this class in the page where the string needs to be filtered

$string = new StringFiltration(); 

Then when filtering or judging strings, the methods defined in the class are called, so some statements for calling methods will appear on the page.

$email = $string->realEscapeString($_POST['email']);
$postId = $string->eregNumber($id);


In the above example, we can see that before object-oriented processing of strings, we must define a class, and then instantiate this class in the required page and call the methods in this class. It seems here that object-oriented Compared with process-oriented, the efficiency is lower, and it is also very troublesome. However, this advantage is also obvious. The statements that actually process or judge strings are written inside the class, and there will be no complexities on the page where the method is called. Custom functions and complex statements such as regular expressions make the structure of the page and even the structure of the entire website clearer. After writing a class, you can use this class again when developing PHP in the future. In the long run The efficiency is actually higher. Therefore, developers who have been doing process-oriented programming in PHP may wish to change their thinking and try object-oriented programming.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1127864.htmlTechArticleSimply understand the object-oriented programming method of PHP. PHP object-oriented programming is different from most object-oriented programming languages. , PHP is a programming method that supports both process-oriented and object-oriented...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn