Speaking of object-oriented, let’s first review the previous programming ideas. The so-called programming ideas are the process of logically reasoning about programs based on the essential principles of knowledge. Programming ideas focus on first clarifying what is to be done. When leaving the code, you must be able to understand how to do this thing. Instead of memorizing the code again. According to what you want to do, confirm various known conditions. If there are no conditions, you must create conditions yourself. That is: know the conditions, know the results, and seek the process. In actual programs, we often have to do a lot of preparation work to create satisfying conditions. For example, to output a piece of data in MySQL, we need to prepare paging calculations to know where to output it, often in a program. One function or one process cannot meet the needs of the entire function. It requires the cooperation of several processes to complete, for example, what to do when the web page is opened, what to do when the form is submitted, what to do when it is not submitted, what to do when the database cannot be connected, and what to do when it is connected. When no form is submitted (when $_POST is empty), we display the form. When data is submitted, we connect to the database, organize the SQL statements, and write them into the database. Then give the user a prompt page. To sum up, when we think about the entire program, we will think about what the program needs to do when the program runs here, and how to do it to meet the needs of the next process. Only the combination of these processes can meet the needs of the entire function. We call this way of thinking process-oriented. To sum up, the process-oriented thinking model has a common feature: when to do something, how to do it, every step of the process. This is process-oriented.
In fact, when we write code, more than 90% of it is process-oriented, while the opposite object-oriented is just the name of a way of thinking. Many times we also use the object-oriented way of thinking but we just don’t know it. The following The code is a simple example:
<span $file</span> = "test.txt"<span ;//指定打开的文件 </span><span $fp</span> = <span fopen</span>(<span $file</span>, "r"<span );//打开文件 </span><span $data</span> = <span fread</span>(<span $fp</span>, 1024<span );//读取数据赋值 </span><span fclose</span>(<span $fp</span><span );//关闭文件 </span><span echo</span> <span $data</span>;//输出
A piece of code corresponds to a process
Many people should be thinking this way when we read the database. //Connect to the database //Query the database //Display the results,
If it is identity verification. There is one more process. //Connect to the database//Query the database//Compare the username and password//Display the results. This comment is actually a description of the idea. When we become proficient in writing code to a certain level, there is no need to think about it line by line. We often think of a process, which is just a few lines of code, but the actual function of the code must be consistent with the thinking process.
Still the above code
<span $fp</span> = <span fopen</span>(<span $file</span>, "r"<span ); </span><span $data</span> = <span fread</span>(<span $fp</span>, 1024<span ); </span><span fclose</span>(<span $fp</span><span ); </span><span //整体注释就是:</span><span 读取“文件”的“内容”<br /><br /></span> //如果我换成另一个同样功能函数呢
<span </span>
This way of writing is more in line with what we just described: reading the file content. Here, when we think about $file, we think of it as a file.
Another example: we can understand the graphics processing process as: creating an image, writing into the image, drawing lines into the image, outputting the image, and treating this amount of resources as the image itself.
The so-called object-oriented is a description of the thinking mode. In this thinking mode, we imagine each thing to be processed as an entity, reading files, processing data and writing files. . Create an image, draw on an image, write on an image, output an image. Although from the nature of the code, they are still various quantities, you just need to be aware of this subconsciously. When thinking and describing, if you think like this one by one. It will bring certain difficulties to thinking. And use the above way of thinking and describing. It’s much simpler. This way of thinking is object-oriented. A sentence similar to the process-oriented one above is: when and who do what.
Process-oriented is: when, what to do, how to do it
Object-oriented is: when, what, do. When object-oriented, there is one less thing about how to do it. That’s because the premise of object-oriented is that you already know how to do it. This is why we always learn process-oriented first. If we don’t know how to do something, we can’t talk about it. What thinking mode. In fact, the image processing function is developed based on the object-oriented thinking model. From the beginning to the end, it is about what to do with this picture. The thinking model is not limited to the writing method. It does not mean that writing this way is object-oriented and writing that way is process-oriented. After we have mastered the implementation methods of various functions. We tend to reuse code through encapsulation. So how can the packaging be more reasonable? At this time, look at the way of thinking, as I said before. The description of the idea must be consistent with the code. Then encapsulation is no longer random encapsulation. When using encapsulated functions and classes, it is best to use them in the same writing format as described by thinking. . and description of ideas. . Match as closely as possible.
<span function</span> read(<span $file</span><span ) { } </span><span //</span><span 读取文件</span> <span $data</span> = read(<span $file</span>);
符合度百分之百,当然前提你得知道怎么封装这样一个函数,在此由于$file是文件名 将它视为文件有点牵强,但我们可以理解为文件的路径path,所以 最好使用类去封装。
用我们日常生活的常识来理解对象的话 对象时一个个实体,那么它对应的就应该有一些特性,比如说文件名是什么,路径是多少,文件大小多少,比如说我们人 有身高体重名字性别,但是,在我们平时的制作手法里面。我们要知道一个文件大小。就必须用 filesize 函数去取得。这就像我问你身高是多少,你每次都要重新量一下。 这和我们常识中的对象,存在一定的差距,使得我们在思考描述代码的时候。代码的符合度不够。 但是类可以暂时记住这些特征值,我们称之为对象的属性,属性,一定是一个准确的值,而过程在类里面称为方法,类里面 可以声明一些特殊的变量,这些变量外部不能直接访问到,这些就是类的属性,要想访问一个类的属性和方法一样使用-> 但是不需要写$,假如我们有一个file类 有一个属性
<span $file</span> = <span new</span> <span file</span><span (); </span><span echo</span> <span $file</span>->size;
用这种方式来访问一个对象变量的属性 怎么定义它 我们先不急 慢慢道来 ,我们先回到思路上,今天我们封装一个文件读写类 我们的代码在需要文件读写的时候我们这样思考:读取文件 处理数据 写入文件 ,这个思路正是文件型计数器的思路。
那么,我们最好的写法是
<span $data</span> = read(<span $file</span><span ); </span><span $data</span> +=1<span ; write(</span><span $file</span>, <span $data</span>);
<span function</span> read(<span $file</span><span ) { </span><span $fp</span> = <span fopen</span>(<span $file</span>, "r"<span ); </span><span $data</span> = <span fread</span>(<span $fp</span>, <span filesize</span>(<span $file</span><span )); </span><span fclose</span>(<span $fp</span><span ); </span><span return</span> <span $data</span><span ; } </span><span function</span> write(<span $file</span>, <span $data</span><span ) { </span><span $fp</span> = <span fopen</span>(<span $file</span>, "w"<span ); </span><span $rs</span> = <span fwrite</span>(<span $fp</span>, <span $data</span><span ); </span><span fclose</span>(<span $fp</span><span ); </span><span return</span> <span $rs</span><span ; }</span>
这两个函数。都是同属于文件操作的。我们把它封装成为类
<span class</span><span fileclass { </span><span function</span> read(<span $file</span><span ) { </span><span $fp</span> = <span fopen</span>(<span $file</span>, "r"<span ); </span><span $data</span> = <span fread</span>(<span $fp</span>, <span filesize</span>(<span $file</span><span )); </span><span fclose</span>(<span $fp</span><span ); </span><span return</span> <span $data</span><span ; } </span><span function</span> write(<span $file</span>, <span $data</span><span ) { </span><span $fp</span> = <span fopen</span>(<span $file</span>, "w"<span ); </span><span $rs</span> = <span fwrite</span>(<span $fp</span>, <span $data</span><span ); </span><span fclose</span>(<span $fp</span><span ); </span><span return</span> <span $rs</span><span ; } }<br /><br /></span>
调用这个类的时候。代码是这么写的。
<span $fc</span> = <span new</span><span fileclass();<br />//读取文件 </span><span $data</span> = <span $fc</span>->read(<span $file</span><span ); </span><span $data</span> +=1<span ;<br />//写入文件 </span><span $fc</span>->write(<span $file</span>, <span $data</span>);
<span public</span> <span function</span> __construct(<span $file</span><span ) { </span><span $size</span> = <span filesize</span>(<span $file</span><span ); }</span>
<?<span php </span><span class</span><span fileclass { </span><span public</span> <span $size</span> = 0<span ; </span><span public</span> <span $name</span> = ''<span ; </span><span public</span> <span function</span> __construct(<span $file</span><span ) { </span><span $size</span> = <span filesize</span>(<span $file</span><span ); </span><span $this</span>->size = <span $size</span><span ; </span><span $this</span>->name = <span $file</span><span ; } </span><span function</span> read(<span $file</span><span ) { </span><span $fp</span> = <span fopen</span>(<span $file</span>, "r"<span ); </span><span $data</span> = <span fread</span>(<span $fp</span>, <span filesize</span>(<span $file</span><span )); </span><span fclose</span>(<span $fp</span><span ); </span><span return</span> <span $data</span><span ; } </span><span function</span> write(<span $file</span>, <span $data</span><span ) { </span><span $fp</span> = <span fopen</span>(<span $file</span>, "w"<span ); </span><span $rs</span> = <span fwrite</span>(<span $fp</span>, <span $data</span><span ); </span><span fclose</span>(<span $fp</span><span ); </span><span return</span> <span $rs</span><span ; } } </span><span $fc</span> = <span new</span> fileclass("test.txt"<span ); </span><span echo</span> "文件名:" . <span $fc</span>-><span name; </span><span echo</span> "文件大小:" . <span $fc</span>-><span size; </span>?>
现在回到read方法 既然他已经有属性 知道自己名字和大小了 那在这里就不用再传文件名进去了,
<span function</span><span read() { </span><span $fp</span> = <span fopen</span>(<span $this</span>->name, "r"<span ); </span><span $data</span> = <span fread</span>(<span $fp</span>, <span filesize</span>(<span $this</span>-><span size)); </span><span fclose</span>(<span $fp</span><span ); </span><span return</span> <span $data</span><span ; } </span>
<span class</span><span fileclass { </span><span public</span> <span $size</span> = 0<span ; </span><span public</span> <span $name</span> = ''<span ; </span><span public</span> <span function</span><span __construct($file) { </span><span $size</span> = <span filesize</span>(<span $file</span><span ); </span><span $this</span>->size = <span $size</span><span ; </span><span $this</span>->name = <span $file</span><span ; } </span><span function</span><span read() { </span><span $fp</span> = <span fopen</span>(<span $this</span>->name, "r"<span ); </span><span $data</span> = <span fread</span>(<span $fp</span>, <span filesize</span>(<span $this</span>->name<span )); </span><span fclose</span>(<span $fp</span><span ); </span><span return</span> <span $data</span><span ; } </span><span function</span> write(<span $data</span><span ) { </span><span $fp</span> = <span fopen</span>(<span $this</span>->name, "w"<span ); </span><span $rs</span> = <span fwrite</span>(<span $fp</span>, <span $data</span><span ); </span><span fclose</span>(<span $fp</span><span ); </span><span return</span> <span $rs</span><span ; } }</span>
<span $fc</span> = <span new</span> fileclass("test.txt"<span ); //读取文件 </span><span $data</span> = <span $fc</span>-><span read(); </span><span $data</span> +=1<span ; </span><span echo</span> <span $data</span><span ; //写入文件 </span><span $fc</span>->write(<span $data</span>);