Php object-oriented. Speaking of object-oriented, let's first review the previous programming ideas. The so-called programming ideas refer to the process and programming ideas of logic reasoning programs based on the essential principles of knowledge, it is important to clarify what to do first. let's first review the previous programming ideas about object-oriented. The so-called programming ideas refer to the process and programming ideas of logical reasoning programs based on the essential principles of knowledge, it is important 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:
= "Test.txt" = (, "r" = (, 1024 (; // output
A piece of code corresponds to a process
When we read the database, many people should think so now. // Connect to the database // query the database // display the result,
For 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.
Or the code above
= (, "R" = (, 1024 (
// What if I change to another function with the same function?
$ File = "test.txt ";
$ Data = file_get_contents ($ file );
This statement is more in line with our previous description: 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: when?WhoWhat to do.
Process-oriented: when, what, and how
Object-oriented: When,What, 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. Encapsulated into a function
read( = read();
Compliance, of course, you have to know how to encapsulate such a function. because $ file is a file name, it is a little far-fetched, but we can understand it as the path of the file, therefore, it is best to use classes for encapsulation.
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 an attribute.
= ->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.
So, our best practice is
= read( +=1, );
read( = (, "r" = (, (( write(, = (, "w" = (, (
These two functions. All belong to file operations. We encapsulate it into a class.
read( = (, "r" = (, (( write(, = (, "w" = (, (
When calling this class. The code is written in this way.
/// Read file =-> read (+ = 1 // write File-> write (,);
However, there is something different from the idea here. the Two $ files in the upper and lower layers can be two different files, that is, I can read the content from file A and write it into file B, but in this case, two files are two objects, which are not in line with the idea. in this code, we cannot accurately describe them. 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.
__construct( = (
We all know that the internal variables 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. You need to use the keyword $ this->
Size =-> name = read (= (, "r" = (, (write (, = (, "w" = (, (= fileclass ("test.txt" "File name :". -> "file size :". ->?>
Now back to the read method, since he already has attributes to know his name and size, there is no need to upload the file name here,
= (->name, "r" = (, (->(
Same. When writing. You do not need to notify the file name.
= 0 = '' = (->size = ->name = = (->name, "r" = (, (->name( write( = (->name, "w" = (, (
Now, the entire class is like this. Return to the counter code.
= fileclass("test.txt" = -> +=1 ->write();
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 ". UltimatelyIn fact, there is a class only by thinking first.
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. we should first clarify what to do...