Php object-oriented

Source: Internet
Author: User
When talking about object-oriented, let's first review the previous programming ideas. The so-called programming ideas refer to the process and programming ideas of the logic reasoning program based on the essential principles of knowledge. What focuses on is to clarify what to do first. When you leave the code, you must be able to understand how to do this. Instead of replacing... "/> <scripttype =" text/javascript "src =" http://www.2cto.c When talking about object-oriented, let's first review the previous programming ideas. The so-called programming ideas refer to the process and programming ideas of the logic reasoning program based on the essential principles of knowledge. What focuses on is to clarify what to do first. When you leave the code, you must be able to understand how to do this. Instead of memorizing the code. Confirm the various known conditions according to what needs to be done, and create the conditions by yourself if there are no conditions. That is, knowing the conditions, knowing the results, and seeking the process. In actual programs, we often have to make a lot of preparations to meet the conditions. for example, to output a piece of mysql data, we need to prepare paging computing to know where to output the data, usually in a program. A function and a process cannot meet the needs of the entire function. It takes several steps to work together. for example, what is done when a webpage is opened, what is done when a form is submitted, and what is not submitted, what will be done when the database cannot be connected. When no form is submitted ($ _ POST is empty), the form is displayed. When data is submitted, we connect to the database, sort SQL statements, and write data to the database. Then, a prompt page is provided. In summary, when thinking about the entire program, we will think about what the program needs to do and how it can meet the needs of the next process. these processes are combined, in order to meet the needs of the entire function, this way of thinking is called process-oriented. the process-oriented thinking model has a common feature: when to do what, how to do it, every step of each process. This is process-oriented. In fact, when we write code, of the above are all process-oriented, and the relative object-oriented is just the name of a way of thinking, in many cases, we will also use the object-oriented method, but we don't know it. the following code is a simple example: $ file = "test.txt "; // specify the open file $ fp = fopen ($ file, "r"); // open the file $ data = fread ($ fp, 1024 ); // value fclose ($ fp) for data reading; // Close the echo $ data file; // output a piece of code corresponds to a process. when we read the database, many people should think so. // Connect to the database // query the database // display the result, if it is identity authentication. There is another process. // Connect to the database // query the database // compare the user name and password // The result is displayed. This comment is actually a description of the idea. when we write code to a certain extent, there is no need to think about a line. A process is often thought of as several lines of code. However, the actual function of the code must be consistent with the thinking process. Copy the code $ fp = fopen ($ file, "r"); $ data = fread ($ fp, 1024); fclose ($ fp ); // The overall comment is: read "content" of "file" // if I change to another function with the same function $ file = "test.txt"; $ data = file_get_contents ($ file ); copying the code is more in line with what we just described: reading the file content. Here, $ file is considered as a file. For another example, we can understand the image processing process as: creating an image, writing to the image, drawing lines to the image, outputting the image, and viewing the amount of resources as the image itself. The so-called object-oriented model is the description of the thinking mode. In this mode, we think of everything we want to process as an entity, reading file processing data and writing it into a file .. Creating images, painting on images, writing on images, and outputting images although interpreted in code, they are still a variety of values, which can be understood by the subconscious. When thinking about and describing, I think about it one by one. It brings difficulties to thinking. This method of thinking and description is used. It's much simpler. This way of thinking is object-oriented. a sentence similar to the process above is: who will do what. Process-oriented: when, what to do, how to do object-oriented: when, what to do, and what to do. How to do this is missing when targeting objects, because the premise of object-oriented is that you already know how to do it, this is also why we always learn the process orientation first. if we do not know how to do one thing, we will not be able to talk about any thinking mode. In fact, the image processing function is based on the object-oriented thinking model. from the very beginning to the end, what is the image? the thinking mode is not limited to the writing mode, this is not to say that writing is object-oriented, that is, writing is process-oriented. after we are proficient in implementing various functions. We often reuse code through encapsulation. So how can we encapsulate it more rationally. At this time, let's look at the way of thinking, as we have said before. The idea description must be consistent with the code. The encapsulation is not arbitrary. the encapsulated functions and classes should be the same as the description .. And train of thought description .. Match as much as possible. For example, I read the file. Encapsulate the function into a function read ($ file) {}// read the file $ data = read ($ file); compliance. of course, you must know how to encapsulate such a function, since $ file is a file name, it is a little far-fetched to regard it as a file, but we can understand it as the path of the file, so it is best to use classes to encapsulate it. If we use the common sense of our daily life to understand objects, objects are objects by entity, then they should have some characteristics, such as what the file name is, what the path is, and how much the file size is, for example, we have a name for height, weight, and gender. However, in our usual production practices. We need to know the size of a file. It must be obtained using the filesize function. This is like asking you how tall you are. you need to repeat it every time. This is a gap with the objects in our common sense, so that we are thinking about describing code. The code does not meet the requirements. However, classes can temporarily remember these feature values, which are called object attributes and attributes. they must be accurate values, and the process is called methods in the class. some special variables can be declared in the class, these variables cannot be directly accessed outside, and these are the attributes of the class. to access the attributes of a class, use them like the method-> but do not need to write $, assume that a file class has a property $ file = new file (); echo $ file-> size; how to define the attribute of an object variable in this way, we don't have to worry about it. let's get back to our ideas first, today, we encapsulate a file read/write class. when our code needs file read/write, we think like this: reading files, processing data, writing data to files, this idea is just the idea of file-type counters. Therefore, our best syntax is $ data = read ($ file); $ data + = 1; write ($ file, $ data); copy the code function read ($ file) {$ fp = fopen ($ file, "r"); $ data = fread ($ fp, filesize ($ file); fclose ($ fp); return $ data ;} function write ($ file, $ data) {$ fp = fopen ($ file, "w"); $ rs = fwrite ($ fp, $ data ); fclose ($ fp); return $ rs;} copies the two functions of the code. All belong to file operations. We encapsulate it into a class Copy code class fileclass {function read ($ file) {$ fp = fopen ($ file, "r"); $ data = fread ($ fp, filesize ($ file); fclose ($ fp); return $ data;} function write ($ file, $ data) {$ fp = fopen ($ file, "w"); $ rs = fwrite ($ fp, $ data); fclose ($ fp); return $ rs ;}} when the code is copied to call this class. The code is written in this way. $ Fc = new fileclass (); // read the file $ data = $ fc-> read ($ file); $ data + = 1; // write the file $ fc-> write ($ file, $ data); however, there is a difference with the idea. the upper and lower $ files can be two different files, that is to say, I can read the content from file A and write it into file B, but in this case, two files are two objects, which is inconsistent with the idea. in this code, we cannot accurately describe it. Which quantity. It can be regarded as this file. Although the class is a thought-oriented process, as mentioned before, an object should have its own attributes, and the object should know its own attributes, we hope to express this object with an instantiated amount. once an object appears, we will know its attributes, such as the names and gender we all know. we need to do this, we need to modify the structure of the class .. That is to say. You need to know the file name at the beginning. In addition, the file size must be read from the beginning. After all, without these processes, it is impossible to get them out of thin air. In the class. Constructors can help us do this. Constructor. It will be executed immediately when the class is instantiated. We can read the file size in the constructor. to read the file size, we also need to know the file name. This requires a condition. It can be passed in through function parameters. Public function _ construct ($ file) {$ size = filesize ($ file);} we all know that the internal and external variables of a custom function. Not in the same world. That is to say. Assign a value to $ size. The attribute size cannot be obtained. Here, in the class method, we need to consider the attributes of the class and other methods. Use the keyword $ this-> Copy code Size = $ size; $ this-> name = $ file;} function read ($ file) {$ fp = fopen ($ file, "r "); $ data = fread ($ fp, filesize ($ file); fclose ($ fp); return $ data;} function write ($ file, $ data) {$ fp = fopen ($ file, "w"); $ rs = fwrite ($ fp, $ data); fclose ($ fp); return $ rs ;}} $ fc = new fileclass ("test.txt"); echo "File name :". $ fc-> name; echo "file size :". $ fc-> size;?> Copy the code and return to the read method. now that he has attributes to know his name and size, you don't need to upload the file name here. copy the code function read () {$ fp = fopen ($ this-> name, "r"); $ data = fread ($ fp, filesize ($ this-> size )); fclose ($ fp); return $ data;} copies the same code. When writing. You do not need to notify the file name. Copy the code class fileclass {public $ size = 0; public $ name = ''; public function _ construct ($ file) {$ size = filesize ($ file ); $ this-> size = $ size; $ this-> name = $ file;} function read () {$ fp = fopen ($ this-> name, "r "); $ data = fread ($ fp, filesize ($ this-> name); fclose ($ fp); return $ data;} function write ($ data) {$ fp = fopen ($ this-> name, "w"); $ rs = fwrite ($ fp, $ data); fclose ($ fp ); return $ rs ;}} copy the current code In, the entire class is like this. Return to the counter code. Copy the code $ fc = new fileclass ("test.txt"); // read the file $ data = $ fc-> read (); $ data + = 1; echo $ data; // write the file $ fc-> write ($ data); copy the code because reading is a process, that is, a method. From where? $ Fc can be fully understood at this time. Is the file itself. Summary: object-oriented thinking. Pay attention to: when, what, and what to do. In order to make code writing more in line with this description method, we need to encapsulate the process. And class. It is only a preparation encapsulation to better conform to this way of thinking and description. it does not mean that classes are used for object-oriented programming. After a class is written. If it is in use. Does not conform to the object-oriented way of thinking .. That's just a common class, object-oriented thinking .. Be sure to have accurate objects .. You can set a certain amount. Something that is regarded as an entity. That is, "object ". In the final analysis, there is actually a kind of thinking first.

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.