PHP Object-oriented (2) _php tutorial

Source: Internet
Author: User
 Note: It is recommended to look at previous articles before reading this article, because the content is more cohesive.   Do not spray.   Object-oriented, is the name of a mode of thinking, not refers to a particular type of writing, object-oriented short-oop, the core of the idea is: when what to do what. Programming fastidious code and the idea of the absolute coincidence, if the code does not correspond to a process of thought, then the idea and code is out of line, then the origin of the code is to be verified, because the object-oriented description, not the specific details of the description, only care about what things do, in order to let code and ideas to match, the package came into Only encapsulation can make a code correspond to what one does, which means that we need to know that we have prepared a bunch of "What to do", either in our own package, or in other people's packaging, in order to make the code more accurately conform to the object-oriented description, we need to use a quantity to pretend to be this thing, Because the process of the final processing of the amount of time, but the average amount is often one by one accurate value, so write out the code, at most can read: read a file to write a file, and not easy to read: What the file does, as before the object-oriented attention to what things do, So usually we will use the instantiated class to represent this object, because there are methods in the instantiated class, so that we can use this instantiation of the class as an object, the method inside the class we can write in the code by this amount of calls inside the method,   is more in line with: what to do. For example, read ($file); We can read: read "This file", here the focus is "read", the file is just read something, and reading is a process. This mode of thinking is also not object-oriented but process oriented. Object-oriented should conform to: what things do. $file->read (); This writing, it is easy to see, is a file-read, is "file" in the read, such a description is more consistent with the object-oriented thinking mode. So. Object-oriented encapsulation is just a description of the code more in line with thought. In order to write, the code is more in line with the thought description. Before we must have seen other people write the class may see some methods, there is only one assignment, which seems to be the egg pain and superfluous writing is actually to use this class when the code seems to be consistent with the thinking, the previous blog on the package of a file class if we have two files each file is considered an object We want to read from file A and then write to File B, A and B are two files, then we use this class to describe this thing. Copy code $ A = new Fileclass ("A.txt"), $b = new Fileclass ("B.txt"); A read out your files $dATA = $a->read (); b Write down this document $b->write ($DATA); echo $b->read (); Copy code complete code copy code as follows size = $size; $this->name = $file; The function read () {$fp = fopen ($this->name, "R"), $data = Fread ($fp, FileSize ($this->name)), fclose ($FP), return $d Ata The 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 out your file $data = $a->read (); b Write down this document $b->write ($DATA); Echo $b->read ();? > Copy Code of course $b->write ($data); You can change its file mode, it's obvious that the first run has an error and then run it again. This class is prepared to implement this description and must know its name in order to make the object look more like a live object. Must know its size, it must be able to "do something", so test.txt b.txt need to exist, for this we need to use a bit more in the package. So is the first use of the idea has a class of encapsulation, a class, to encapsulate the description of the usage is a qualified class, for this official launch of a class library pear, provides a lot of pre-packaged classes, we have to use the good, if we know a class from somewhere else how should we use it? The first step is to know what the class is and what object is being developed for. If a class is not developed for any object, it is just a way of encapsulation, and when the custom function is used. The second step: Know this class, all for our object, provides what properties and methods, through the properties, we can get the object of what parameters: or change the property value. We can let the object produce what kind of change, have what method, these methods, can let the object do what, concrete how to do, do not have the tube, if you are to learn how to do this process, you can go to see other people's Class source code. Now let's take a look at the previous content with code separation, which is template technology. The principle is to write all the control output code in another file, need to output the time to be included, the main point is: template file variables to have strict conventions, must knowTao template has what can be output variables, what kind of variable, in order to accurately assign the value up. There is another template principle is a labeled template, on the template, do a variety of marks. Then, replace the variable and tick one by one that you want to assign. In order to avoid variable name collisions, we tend to put the variables in the other place first. To display, retrieve these variables, process them, and assign them to the template. Whether it's a direct assignment or a replacement. In order to facilitate the management of these processes, we will generally choose encapsulation. One is to make it easier to reuse code. The second is to isolate the variables of the current code, so, there is a variety of different ideas produced template encapsulation. Others are packaged well. We know what the principle is, but it's all about replacing variables or assigning values directly. Then again mentioned the Smarty ... Smarty content See next article:

http://www.bkjia.com/PHPjc/756541.html www.bkjia.com true http://www.bkjia.com/PHPjc/756541.html techarticle Note: It is recommended to look at previous articles before reading this article, because the content is more cohesive. Do not spray. Object-oriented, is the name of a mode of thinking, does not mean a particular ...

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