Php object-oriented (1)

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 memorizing the code. Confirm various known items with the data to be done

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 various known items with the data to be done

AboutOrientedObjectFirst, let's 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, this way of thinking is calledOrientedProcess,OrientedThe thinking model has a common feature: when to do, how to do, and every step of the process. This isOrientedProcess.

In fact, when we write code, or more of them areOrientedProcess, and relativeOrientedObjectIt's just a way of thinking. We often useOrientedObjectThe following code is a simple example:

$ File = "test.txt"; // specify the opened file $ fp = fopen ($ file, "r"); // open the file $ data = fread ($ fp, 1024); // value assigned to read data: fclose ($ fp); // close the file echo $ data; // 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

$ Fp = fopen ($ file, "r"); $ data = fread ($ fp, 1024); fclose ($ fp); // The overall comment is: read "content" of "file"

// 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-calledOrientedObjectIt is the description of the thinking mode. In this mode, we think of every thing to be processed as an entity, reading the file processing data and writing it into the 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 isOrientedObject, Similar to the aboveOrientedThe process is like this: When?WhoWhat to do.

  OrientedThe process is: when, what, and how
  OrientedObjectYes: When,What, What to do. InOrientedObjectWhy is one missing? That's becauseOrientedObjectThe premise is that you already know how to do it, which is why we always learn first.OrientedIf we don't know how to do one thing in the process, we won't be able to talk about the thinking mode. In fact, the image processing function is based onOrientedObjectThe Thinking Mode is developed from the beginning to the end. The thinking mode is not limited to the writing mode.OrientedObjectWriteOrientedProcess, after we are proficient in the implementation of 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

Function read ($ file) {}// read the file $ data = read ($ file );

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.

Understanding with the knowledge of our daily lifeObjectWordsObjectWhen an object is created, it corresponds to some features, such as the file name, path, and file size. For example, we have a height, weight, name, 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 in common senseObject, There is a certain gap, so that we are thinking about describing the code. The Code does not meet the requirements. However, classes can temporarily remember these feature values, which we callObjectThe attribute and attribute of must be an accurate value, and the process is called a method in the class. Some special variables can be declared in the class. These variables cannot be directly accessed outside the class, 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.

$file = new file();echo $file->size;

AccessObjectLet's look at how to define the attribute of a variable. Let's get back to our thinking. Today we encapsulate a file read/write class. Our code thinks like this when we need file read/write: reading file processing data and writing data to files is the idea of file-type counters.

So, our best practice is

$data = read($file);$data +=1;write($file, $data);

    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;    }

These two functions. All belong to file operations. We encapsulate it into a class.

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 calling 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 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 way, there are two files, that is, two files.ObjectThis is not in line with the idea. In this code, we cannot accurately describe it. Which quantity. It can be regarded as this file. Although the class is usedOrientedAs mentioned earlierObjectShould have its own properties,ObjectWe should know our own properties, and we want to use an instantiated amount to represent thisObject, OneObjectOnce we see this, we will know our attributes, such as the names and gender we all know. To do this, we need to modify the class structure and know it from the very beginning .. 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 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 = $ 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;?>

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,

    function read() {        $fp = fopen($this->name, "r");        $data = fread($fp, filesize($this->size));        fclose($fp);        return $data;    }    

Same. When writing. You do not need to notify the file name.

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;    }}

Now, the entire class is like this. Return to the counter code.

$ Fc = new fileclass ("test.txt"); // read the file $ data = $ fc-> read (); $ data + = 1; echo $ data; // write the file $ fc-> write ($ data );

Reading is a process, that is, a method. From where? $ Fc can be fully understood at this time. Is the file itself.

  Summary:OrientedObjectThis way of 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 be more accurate in line with this way of thinking description, not to say that the class is in useOrientedObjectProgramming. After a class is written. If it is in use. Non-conformingOrientedObject.. It's just a common class,OrientedObjectThe way of thinking .. Must be accurateObject.. 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.

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.