A brief understanding of PHP's object-oriented programming method _ php Basics

Source: Internet
Author: User
Tags php basics
Although PHP is only designed to run on server software, it still contains many object-oriented elements, such as classes and methods. This article takes beginners to understand PHP's object-oriented programming method. unlike most object-oriented programming languages, PHP supports both process-oriented and object-oriented programming, PHP developers can choose either one or both process-oriented and object-oriented. However, in versions earlier than PHP5, PHP is mainly a process-oriented programming language, therefore, most PHP developers should choose the process-oriented method for development. In fact, Kayo believes that even if a PHP developer does not use the object-oriented method at all, he can develop excellent PHP programs, as we can imagine, the parsing of Web pages is very procedural. embedding process-oriented code in HTML is a natural means, therefore, it cannot be said that object-oriented is a better programming method than process-oriented, but another programming option. of course, this is the case in PHP.

For the advantages and disadvantages of process-oriented and object-oriented in PHP, I believe it will be clear after a slight check on the internet. the process-oriented development cycle is short, the release is fast, and the efficiency is high, the object-oriented development cycle is long, inefficient, but easy to maintain, improve, expand, and develop APIs. Obviously, it is easy to see. it is hard to say which method will be better. instead of arguing which programming method is better, it is better to give full play to the respective advantages of the two programming methods.

Back to PHP's object-oriented programming, it is easy to feel its advantages when using object-oriented programming. The most obvious thing is that the code functions are clearer, data processing, and user login, the content presentation and so on are written into a class. on the page, you only need to include these classes, instantiate the object, and then use a simple statement to apply the object. this is in line with the process-oriented data processing, user login, compared with writing other content together, the former programming idea must be clearer and easier to understand, and I believe that team development should be more inclined to object-oriented programming.

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

When processing forms or accepting url parameters, PHP developers often need to filter strings to prevent SQL injection and other problems.

In the process-oriented approach, we will call various library functions or custom functions to filter strings in the statements that need to filter strings. in this way, there will be many different filtering functions and even complex regular expressions on the page. even if you write enough comments on the page, it will be confusing. let's look at the object-oriented processing method.

The first is to define a simple class for processing strings and write various complicated string processing methods (PHP object-oriented knowledge can be Google, which is not described in this article .)

<? Php/* string processing class * parameter $ length is used to determine whether the string exceeds the specified length * special characters in the character strings used in the escape SQL statement * regular limit strings can only be numbers * determine whether the string is null * judge string length * // create a string processing class StringFiltration {// attribute var $ length; // method // Constructor function _ construct ($ the_length = NULL) {$ this-> length = $ the_length ;} // escape the special character function realEscapeString ($ the_string) {return mysql_real_escape_string ($ the_string) in the character string used in the SQL statement;} // the regular expression can only contain numbers in the function eregNumber ($ The_string) {if (ereg ("^ [0-9] + $", $ the_string) return true; else return false ;} // judge whether the string is null function strlenString ($ the_string) {return strlen ($ the_string);} // judge the string length function ifOverStrlenLength ($ the_string) {if (strlen ($ the_string) >$ this-> length) return true; else return false ;}}?>

Then instantiate the class on the page where the string needs to be filtered

$string = new StringFiltration(); 

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

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


In the above example, we can see that we must define a class before object-oriented processing of strings, and then instantiate this class on the required page and call the methods in this class, it seems that the object-oriented efficiency is lower than the process-oriented, and it is also very troublesome, but this advantage is also obvious, the actual processing or judgment of string statements are written inside the class, on the page that calls the method, there will be no complicated user-defined functions and complex statements such as regular expressions. The structure of the page and the entire website are clearer, after writing a class, you can use this class for PHP development in the future. in the long run, the efficiency is high. Therefore, developers who have been conducting PHP process-oriented programming may wish to try the object-oriented approach.

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.