Object-oriented php_PHP tutorial

WBOY
Release: 2016-07-13 10:32:40
Original
723 people have browsed it

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>;//输出
Copy after login

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 读取&ldquo;文件&rdquo;的&ldquo;内容&rdquo;<br /><br /></span> //如果我换成另一个同样功能函数呢
Copy after login
​$file = "test.txt";
$data = file_get_contents($file);
<span  </span>
Copy after login

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>);
Copy after login

符合度百分之百,当然前提你得知道怎么封装这样一个函数,在此由于$file是文件名 将它视为文件有点牵强,但我们可以理解为文件的路径path,所以 最好使用类去封装。

  用我们日常生活的常识来理解对象的话 对象时一个个实体,那么它对应的就应该有一些特性,比如说文件名是什么,路径是多少,文件大小多少,比如说我们人 有身高体重名字性别,但是,在我们平时的制作手法里面。我们要知道一个文件大小。就必须用 filesize 函数去取得。这就像我问你身高是多少,你每次都要重新量一下。 这和我们常识中的对象,存在一定的差距,使得我们在思考描述代码的时候。代码的符合度不够。 但是类可以暂时记住这些特征值,我们称之为对象的属性,属性,一定是一个准确的值,而过程在类里面称为方法,类里面 可以声明一些特殊的变量,这些变量外部不能直接访问到,这些就是类的属性,要想访问一个类的属性和方法一样使用-> 但是不需要写$,假如我们有一个file类 有一个属性

<span $file</span> = <span new</span> <span file</span><span ();
</span><span echo</span> <span $file</span>->size;
Copy after login

用这种方式来访问一个对象变量的属性 怎么定义它 我们先不急 慢慢道来 ,我们先回到思路上,今天我们封装一个文件读写类 我们的代码在需要文件读写的时候我们这样思考:读取文件 处理数据 写入文件 ,这个思路正是文件型计数器的思路。

那么,我们最好的写法是

<span $data</span> = read(<span $file</span><span );
</span><span $data</span> +=1<span ;
write(</span><span $file</span>, <span $data</span>);
Copy after login
    <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>
Copy after login

这两个函数。都是同属于文件操作的。我们把它封装成为类

<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>
Copy after login

调用这个类的时候。代码是这么写的。

<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>);
Copy after login
然而这里有个和思路不符的地方,上下的两个$file可以是两个不同的文件,也就是说我可以从文件A读取内容写入到文件B中,但是这样一来就是两个文件,就是两个对象,这个和思路不符,在这个代码中,我们没办法准确的描述出。哪一个量。可以视为这个文件。 尽管使用了类从思维上还是面向的过程,之前说过 作为对象应该有自己的属性,对象 应该知道自己的属性,我们希望 用一个实例化的量 来表示这个对象,一个对象 一旦出现就知道自己的属性,就如我们都知道的姓名和性别,要做到这几点,我们需要修改类的结构,一开始就知道。。就是说。一开始就得知道文件名。而且一开始就要读取文件大小。 毕竟,没有这些过程 不可能凭空得到。在类里面。构造函数可以帮我们做到这一点。构造函数。会在类实例化的时候立即执行。我们可以在构造函数里读取文件的大小,要读取文件大小,同样需要知道文件名。 这就需要一个条件。可以通过函数参数传入。
   <span public</span> <span function</span> __construct(<span $file</span><span ) {
        </span><span $size</span> = <span filesize</span>(<span $file</span><span );
    }</span>
Copy after login
我们都知道,自定义函数内部变量和外部变量。不是同一个世界的。 也就是说。在这里给 $size 赋值。属性 size 是得不到的。 在这里 在类的方法里面,要想访问类的属性和其他方法。需要用关键字 $this->
<?<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>?>
Copy after login

现在回到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>
Copy after login
同样的。写入的时候。也不需要再通知文件名了。
<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>
Copy after login
现在,整个类就变成了这个样子。 回到刚才的计数器代码。
<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>);
Copy after login
 由于读取是一个过程,也就是一个方法。从哪读取的? $fc  $fc 此时,可以完全的理解为。就是文件本身了。   总结下:面向对象这种思维方式。讲究的是:什么时候,什么东西,做什么 。为了能让代码书写的时候,更符合这种描述方式,我们需要把过程封装起来。而类。只不过是为了能更准确的符合这种思考描述方式而做的准备性封装,不是说用类就是在用 面向对象 编程了。一个类写出来以后。如果在使用的过程中。不符合面向对象的思维方式。。那也只是普通的类,面向对象思维方式。。一定要有准确的对象。。可以把某个量。视为一个实体的东西。也就是“对象” 。归根到底其实是先有思路才有类的。

 

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/755381.htmlTechArticleWhen it comes to object-oriented, let’s review the previous programming ideas. The so-called programming ideas are logical reasoning procedures based on the essential principles of knowledge. The process and programming ideas focus on clarifying what to do first...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!