Home  >  Article  >  Backend Development  >  Object-oriented php (2)_PHP tutorial

Object-oriented php (2)_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:32:13797browse

Note: Before reading this article, it is recommended to read the previous articles, because the content is more connected. Do not spray. .

Object-oriented is the name of a mode of thinking. It does not refer to a specific way of writing. Object-oriented is referred to as oop. The core of the idea is: when, what, and what.
Programming pays attention to the absolute consistency between the code and the idea. If the code does not correspond to a certain process of the idea, then the idea is out of touch with the code, and the origin of the code needs to be verified. Due to the object-oriented description, specific details are not cared about. Description only cares about what something does. In order to make the code and ideas match, encapsulation comes into being. Only encapsulation can make a sentence of code correspond to what it does. This means that we need to know that we have prepared a bunch of "what to do" in advance. ", either encapsulate it yourself, or use someone else to encapsulate it. In order to make the code more accurately conform to the object-oriented description, we need to use each quantity to imagine what it is, because the final processing time of the program, but ordinary Quantities are often accurate values ​​one by one. The code written in this way can at most be read as: read a file and write a file, but it is not easy to read as: what does the file do? As mentioned before, object-oriented emphasizes When does something do something, so usually we use the instantiated class to represent the object, because the instantiated class has methods, so when we use the instantiated class This quantity can be regarded as an object. We can use this quantity to call the methods in the class when writing code, which is more consistent with: what does something do.
For example: read($file); We can read it as: read "this file", the focus here is "reading", the file is just something to be read, and reading is a process. This mode of thinking cannot be called object-oriented but process-oriented. Object-oriented should be consistent with: what does something do. $file->read(); This way of writing, it is easy to see that it is file->read, and it is the "file" that is reading. This description is more in line with the object-oriented thinking mode. So said. Object-oriented encapsulation is just to make the code more consistent with the description of thinking. In order to write, the code is more consistent with the description of thinking. We must have seen classes written by others before and may have seen some methods with only one assignment in them. This may seem painful and redundant, but it is actually so that the code can look consistent with the thinking when using this class. A file class encapsulated in the previous blog. If we have two files, each file is regarded as an object. We want to read the content from file A and then write it to file B. A and B are two files respectively. Then we will use this class to describe this matter.
Copy code
$a = new fileclass("a.txt");
$b = new fileclass("b.txt");
//A reads out its own file
$data = $a->read();
//BNote this file
$b->write($data);
echo $b->read();
Copy code
The complete code is as follows
Copy 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, $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 reads out its own file
$data = $a->read();
//BNote this file
$b->write($data);
echo $b->read();
?>
Copy code
Of course $b->write($data); can change its file mode. Obviously there is an error in the first run and run it again to see. This class is prepared to implement this description. In order to make the object To look more like a living object, it must know its own name. You must know its size, and it must be able to "do something", so test.txt b.txt needs to exist. For this reason, we need to put more thought into encapsulation. Therefore, we first have the idea of ​​​​using it before we can encapsulate the class. A class must be encapsulated in a way that meets the usage description to be a qualified class. For this reason, the official launch of a class library pear provides a large number of pre-encapsulated packages. We can just use it. If we know a class from somewhere else, how should we use it? The first step is: know what object this class is developed for. If a class is not developed for any object, then it is just a way of encapsulation and can be used as a custom function. Step 2: Know what attributes and methods this class provides for our objects. Through the attributes, we can get what parameters of the object. . Or change the attribute value. What kind of changes can we make to the object, and what methods are there. These methods can make the object do something. You don’t need to worry about how to do it specifically. If you want to learn how to do this process, you can go to Take a look at the source code of someone else's class. Now let's experience it. The previous content has code separation, which is template technology. The principle is to write all the code that controls the output in another file, and then include it when output is needed. The key point is: the variables in the template file must have strict conventions, and you must know what variables can be output in the template and what they look like. Variables can be assigned values ​​accurately. There is another template principle which is the mark type template, where various marks are made on the template. Then, replace the variables and tokens to be assigned one by one. In order to avoid variable name conflicts, we often temporarily store variables in another place. When it is time to display, these variables are retrieved, processed, and assigned to the template. Whether it is directly assigning a value or replacing it. In order to facilitate the management of these processes, we generally choose encapsulation. One is to facilitate code reuse. The second is to isolate the variables of the current code, so template encapsulation using various ideas has emerged. Packaged by others. As long as we know the principle, it's nothing more than replacing variables or assigning values ​​directly. So smarty was mentioned again. . . See the next article for smarty content. .

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/756541.htmlTechArticleNote: Before reading this article, it is recommended to read the previous article, because the content is more connected. Do not spray. . Object-oriented is the name of a mode of thinking. It does not refer to a specific...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn