PHP's Object-oriented _php tutorial

Source: Internet
Author: User
Referring to object-oriented first review of the previous programming ideas, the so-called programming is based on the essence of knowledge through the process of logical reasoning procedures, programming ideas, pay attention to the first clear what to do. When you leave the code, you should be able to understand how to do it. Instead of putting the code back through. According to the things to do, to confirm the various known conditions, without conditions to create conditions for themselves. That is: Know the condition, know the result, and seek the process. In the actual program we often have to do a lot of preparation to create satisfying conditions, such as the output of MySQL a piece of data we need to prepare a paging calculation, only know where to output from where, often a program. A function, a process, does not meet the needs of the entire function. It takes several process mates to complete, such as what to do when the page is open, what to do when the form is submitted, what to do without submission, what to do when the database is not connected. When there is no form submission ($_post is empty), we display the form. When data is submitted, we connect to the database, organize the SQL statements, and write to the database. Then give the user a prompt page. Summing up, our entire program in the thinking of the time when the program to run to this time what the program needs to do, how to do, in order to meet the needs of the next process, these several processes to meet the needs of the entire function, this way of thinking, we call the process, process-oriented summary down Thinking patterns have a common feature: When to do what, how to do, every step of the process. This is the process oriented.

In fact, when we write code more than 90% are process-oriented, and the relative object-oriented is just a way of thinking of the name, a lot of time we will also use object-oriented way of thinking just don't know it, the following code is a simple example:

< span="">=< span="">< span="">"test.txt"< span="">(, "R  < span=""> "< span="">  < span=""> (, 1024x768  < span="">  < span="">  < span=""> < span="">  < span="">(< span="">;//Output< span="">< span="">

A section of code corresponds to a process

When we read the database, a lot of people should think like that now. Connect database//Query database//display the results,

If it is authentication. There is one more process. Connect database//Query database//compare user name and password//display results. This annotation is actually a description of the idea, when we write code proficiency to a certain extent, there is no need to go to a line of thinking. Often think of a process, just a few lines of code, but the actual function of the code must be consistent with the process of thinking.

Or a piece of code above

< span="">< span="">(< span="">, "< span="">< span="">R"(,< span="">1024< span=""> < span="">< span="">(< span="">< span="">< span="">

What if I change to another same function?
$file = "Test.txt";
$data = file_get_contents ($file);
< span="">

This is more in line with what we just described: Read the contents of the file. Here, $file we think about it, consider it a document.

Another example for the graphics processing process we can understand: Create an image, write to the image, draw lines in the image, output the image, the amount of this resource as the image itself.

The so-called object-oriented is the description of the mode of thinking, in this mode of thinking we think of the various things to be processed into entities, read file processing data written to the file. Create an image on an image to paint on an image write output image although it is from the nature of the code to read them, they are a variety of quantities, this point of their subconscious clearly can be. When thinking and describing it, if you think of it that way. will bring some difficulties to the thinking. And using this way of thinking and describing. It's much simpler, and this way of thinking is object-oriented, like the one in the process above: when and who does what.

Process-oriented: when, what to do, how to do
Object-oriented is: When, what , what to do. When it comes to object-oriented, it's because the object-oriented premise is that you already know how to do it, and that's why we always learn the process first if we don't know how to do something, there's no way to think about it. In fact, for the image processing function is based on the object-oriented thinking pattern development from beginning to end is what to do with this picture, thinking mode, not limited to writing, not to say that this is object-oriented writing is the process, in our mastery of the various functions of the implementation of the method after. We often use encapsulation to reuse code. So how to encapsulate more reasonable. This time, look at the way of thinking, said before. Describe the idea, to be able to match the code. Then the package is not a random encapsulation, packaged functions and classes, it is best to use the same as the thinking described in the writing format. and thought description. Match as much as possible.

For example, I took the process of reading the file just now. Encapsulated as a function
< span=""> < span="">    Read (< span="">    < span="">  = Read (< span="">    < span="">  < span="">);

Compliance 100%, of course, you have to know how to encapsulate such a function, in this because $file is a file name that treats it as a bit farfetched, but we can understand the path to a file, so it's best to use classes to encapsulate.

Use our daily life common sense to understand the object of the object when an entity, then it should have some characteristics, such as the filename is what, the path is how much, the size of the file, for example, we have the height and weight of the name of gender, but in our usual production method inside. We want to know a file size. You must use the FileSize function to obtain it. It's like I asked you how tall you are, and you have to measure it again every time. There is a gap between this and the object of our common sense, which makes us think about describing the code. The code is not compliant enough. But the class can temporarily remember these eigenvalues, what we call an object property, the property, must be an accurate value, and the procedure in the class is called the method, the class inside can declare some special variables, these variables are not directly accessible to the outside, these are the properties of the class, to access the properties and methods of a class to use the same- > but does not need to write $ if we have a file class that has a property

< span="">< span="">< span="">< span="">< span="">< span="">->size;

In this way to access the properties of an object variable how to define it we do not hurry slowly, we first go back to the idea, today we encapsulate a file read and write class our code in the need to read and write the file when we think: Read file processing data to write files, this idea is the file-type counter thinking.

Well, our best way to do that is

< span="">= Read (  < span="">    < span="">  +=1  < span="">   < span="">   < span="">< span="">);
     Read (
    
      =  ( , "R" 
        
          =  (,  ( 
              
                (
                 
                    write (, 
                        
                          = Span fopen< span> (, "W" 
                           
                             =  (, span $data span>
                               
                                 (
                                  
                                    
                                      
                                       
                                    
                                  
                             
                           /span>    
                   
                   
           
      

These two functions. belong to the same file operation. We encapsulate it as the class

 Read (
      
        =  ( , "R" 
          
            =  (,  ( 
                
                  (
                   
                      write (, 
                          
                            = Span fopen< span> (, "W" 
                             
                               =  (, span $data span>
                                 
                                   (
                                    
                                      
                                        
                                        
/span>

When calling this class. This is what the code says.

< span="">< span="">
Read file < span=""> ->read ( < span=""> < span=""> < span=""> < span=""> +=1
Write file < span=""> < span="">->write (< span="">);
However, there is a line of thinking, the top and bottom of the two $file can be two different files, that is, I can read from the file a write to file B, but this is two files, two objects, this and ideas do not match, in this code, we have no way to accurately describe. Which quantity. Can be considered as this file. Although the use of the class from the thinking of the process, previously said as an object should have its own properties, the object should know its own properties, we want to use an instanced amount to represent the object, once an object appears to know their properties, as we all know the name and gender, to do this, We need to modify the structure of the class, as we know from the beginning. So. You need to know the file name from the beginning. And the file size will be read at the beginning. After all, no such process is impossible to get out of thin air. Inside the class. Constructors can help us do this. constructor function. Executes immediately when the class is instantiated. We can read the size of the file in the constructor, to read the file size, we also need to know the filename. This requires a condition. Can be passed in by function arguments.
   < span="">< span="">__construct< span="">(< span="">(< span="">< span="">< span="">< span="">
As we all know, custom function internal variables and external variables. Not the same world. Other words. Assign a value to $size here. The property size is not get. Here in the method of the class, you want to access the properties of the class and other methods. Keyword $this required
  = 0 = '' __construct (=(->size =->name =Read=(, "R"=(, (
                                    
                                      (
                                       
                                         
                                          
                                            write (, 
                                              
                                                =  (, "w" 
                                                  
                                                   < span $rs span> = 
                                                    (, 
                                                      
                                                        ( span $fp span>
                                                        
                                                          
                                                           
                                                              fileclass ("test.txt" 
                                                              
                                                                "file name:". -> "File size:". ->?>       
                                                                
                                                             
                                                         
                                                      
                                                  
                                              
                                          
                                       
                                     

Now go back to the Read method since he already has properties to know his name and size, then there is no need to pass the file name in it,

    < span="">< span="">< span="">< span="">(< span="">->name, "R"< span="">< span="">(< span="">  (- < span="">  < span=""> < span="">  < span="">  < span=""> (< span="">< span="">< span="">< span="">< span="">
The same. When writing. There is no need to notify the file name again.
 = 0 = '' =(->size =->name =
                      
                        =  ( ->name, "R" 
                            
                              =  (,  ( Span $this (Span>->name
                                 
                                   (
                                     
                                      
                                       
                                         write (
                                           
                                            =  (->name, "w" 
                                              
                                                =  (, 
                                                   
                                                     (
                                                       
                                                        
                                                          
                                                           
                                                        
                                                       
                                               /span>   
                                            
                                         
                                      
                                     
                               
                      
Now, the whole class becomes this way. Go back to the counter code just now.
< span="">< span="">Fileclass ("< span="">< span="">< span="">test.txt  " < span="">    +=1  < span=""> < span="">  < span=""> < span="">  < span="">   < span=""> ->write (< span="">);
Because reading is a process, that is, a method. Where did you read it from? $FC $FC At this point, it can be fully understood as. is the file itself. In Conclusion: Object-oriented thinking mode. Pay attention to: when, what, what to do. In order for the code to be written in such a way that it is more consistent with this description, we need to encapsulate the process. and class. It's just a prep package for a more accurate approach to this kind of thinking description, not to say that classes are programmed with object-oriented programming. After a class is written out. If the process is in use. does not conform to the object-oriented way of thinking. That's just the normal class, the object-oriented way of thinking. Be sure to have the exact object. Can put a certain amount. As something of an entity. That is, "object." in the final analysis, there is a kind of thinking first.

http://www.bkjia.com/PHPjc/755381.html www.bkjia.com true http://www.bkjia.com/PHPjc/755381.html techarticle referring to the object-oriented first review of the previous programming ideas, the so-called programming thinking is based on the essence of knowledge through the process of logical reasoning procedures, programming ideas, pay attention to the first clear to do ...

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