Php object-oriented (2) _ PHP Tutorial

Source: Internet
Author: User
Php object-oriented (2 ). Note: Before reading this article, you are advised to read the previous article because the content is highly compatible. Do not spray .. Object-oriented is a kind of thinking model name, not a specific note: Before reading this article, we suggest you look at the previous article, because the content has a strong relationship. Do not spray ..

Object-oriented is the name of a thinking model. it does not refer to a specific writing method. object-oriented is short for oop. The core of the idea is: when and what to do.

The code and thinking are absolutely consistent in programming. if the code does not have a specific process of thinking, the idea will be out of line with the code, so the origin of the code is to be verified, because of the object-oriented description, we do not care about the specific details, but what we do. in order to make the code and ideas consistent, encapsulation came into being, only encapsulation can make a sentence of code correspond to one, which means that we need to know what we have prepared in advance, either encapsulated by ourselves or encapsulated by others, in order to make the code more accurate and conform to the object-oriented description, we need to use a number to imagine what it is, because the amount of time the program finally processes, however, the common amount is often accurate values. the code written in this way can be read at most: reading a file and writing it into a file, which is not easy to read: what the file is doing, as we mentioned earlier, object orientation focuses on when and what to do, so we usually use the class after instantiation to represent this object, because there are methods in the instantiated class, we can regard this quantity as an object when using the instantiated class. the methods in the class are written in the code. You can call the methods in this volume to better match the following: what is done.

For example: read ($ file); we can read it as: read this file. the focus here is "read". the file is only read, but reading is a process. This thinking mode is not called object-oriented but process-oriented. Object-oriented should be consistent with: what is done. $ File-> read (); this method is easy to see. it is a file-> read, and a "file" is being read, this description is more in line with the object-oriented thinking model. So. Object-oriented encapsulation only serves to better conform to the thinking description of the code. In order to write, the code is more in line with the description of thinking. Before that, we must have read the classes written by others and may see some methods. there is only one assignment in them, this seemingly painful and redundant way of writing is actually to make the code look consistent with the thinking when using this class, in the previous blog, A file class is encapsulated. if we have two files and each file is regarded as an object, we want to read the content from file A and then write it into file B, A and B are two files respectively, so we will use this class to describe this thing.

$ A = new fileclass ("a.txt ");

$ B = new fileclass ("B .txt ");

// A. read the file.

$ Data = $ a-> read ();

// B write down this file

$ B-> write ($ data );

Echo $ B-> read ();

The complete code is as follows:

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, $ op = 'a '){

$ Fp = fopen ($ this-> name, $ op );

$ Rs = fwrite ($ fp, $ data );

Fclose ($ fp );

Return $ rs;

}

}

$ A = new fileclass ("test.txt ");

$ B = new fileclass ("B .txt ");

// A. read the file.

$ Data = $ a-> read ();

// B write down this file

$ B-> write ($ data );

Echo $ B-> read ();

?>

Of course, $ B-> write ($ data); can change its file mode. it is obvious that the first time you run an error, you can run it again. this class is prepared to implement this description, to make an object look more like a living object, it must know its own name. When we look at the big and small ones, we can just click here, so test.txt B .txt needs to exist. Therefore, we need to think more about it during encapsulation. Therefore, the idea of using the class is the encapsulation of the class. a class is qualified for the usage of the description to be encapsulated. For this reason, a class library pear is officially launched, we have provided a lot of pre-encapsulated classes, so we can use them. if we know from other places How should we use a class? The first step is to know what object this class is intended. If a class is not developed for any object, it is only an encapsulation method. it is good for user-defined functions. Step 2: Know what attributes and methods this class provides for our objects. through properties, we can get the parameters of the objects .. Or change the attribute value. We don't need to worry about the changes that can be made to an object, the methods that can be used to make the object do anything, if you want to learn how to do this, you can look at the source code of others' classes. Now let's try it out. the previous content includes code separation, that is, template technology. The principle is to write all the control output code in another file and include it when output is required. The key point is that the variables in the template file must have strict conventions, you must know what variables can be output in the template, and what variables can be accurately assigned. Another template principle is tag-type templates, which provide various marks. Then, replace the variables and marks to be assigned values one by one. To avoid variable name conflicts, we often store variables in another place first. The variables are Retrieved when they are to be displayed. after processing, they are assigned to the template. Whether it is directly assigned or replaced. To facilitate management of these processes, we generally choose encapsulation. First, to facilitate repeated use of code. Second, in order to isolate the variables of the current code, there is a template encapsulation created using different ideas. Encapsulated by others. We can understand the principle. it is nothing more than replacing variables or assigning values directly. So I mentioned smarty again... For more information about smarty, see the next article ..

Bytes. Do not spray .. Object-oriented is the name of a thinking model, not a specific...

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.