After procrastinating for two days, I finally have some time to write the next chapter tonight. However, facing the computer, I don’t know where to start. Maybe, just follow ZEND FRAMEWORK. Of course, I will grasp the key points. Remember that this article uses zend framework as an example to explain my understanding of object-oriented programming, rather than an introductory manual for zend framework. Moreover, this will not be an introduction to object-oriented programming. Tutorial, but my understanding of object-oriented.
02 1. Unified entry file
03 In the eyes of many people, PHP is still not "completely" object-oriented. The reason is that object-oriented should mean that all programs must be objectified, and PHP still has an entry file. But this kind of academic debate actually has nothing to do with us. We only need to know that as a completely object-oriented application, except for index.php, all programs should be written in classes.
04 In zend framework, rewrite technology is used. Rewrite not only uses the pseudo-static function to attract search engine inclusion, but also prohibits PHP files that are indexed from running directly to avoid security risks. In joomla, rewrite can be selected in the background. Even for process-oriented projects, discuz and phpwind both provide rewrite options. In zend framework, it may make many zf beginners vomit blood. Practical or not using rewrite, there is no need to configure it at all, just the URL address of the link is different. It's strange that no information seems to mention this. In fact, we can use zend framework under IIS and on most virtual hosts that support PHP without having to worry about rewrite.
05 Of course, I personally strongly support rewrite technology. For me, this technology has at least three benefits: 1. Use pseudo-static method to increase search engine indexes; 2. Protect important files and dangerous files from be run and downloaded; 3. When using PHP to generate image files, the URL address and the real image address format are exactly the same.
06 Therefore, a good object-oriented program should unify the entry files and use the rewrite function to limit the use of other non-standard PHP files.
07 2. MVC pattern
08 MVC is a very classic programming method. I have already mentioned in the previous article of this article that if we purely pursue execution efficiency, one file per program page is the most efficient. But manpower has its limits, so it is necessary to cut the files into pieces and classify them according to certain rules. If you have been doing PHP for long enough, you should have seen all kinds of cutting and file classification methods. Among them, the only one that has been sublimated into a pattern and accepted by the mainstream is MVC.
09 If it is generally accepted that the program and the page are separated, that is, the view is independent, then many people still have different opinions on whether the controller and the model should be separated. At first, I thought that there was no need to be so particular about small projects, but soon I discovered the benefits of models. Models are largely similar in function to function libraries in process-oriented projects. The benefits of function libraries, I have already said at the beginning of the previous article. These reasons are also applicable to models. Functions should always be subdivided into functions. Yes, MODEL is just a unified classification of these functions. The main difference between a model and a function library is that we usually build a model for each data table, so that there are not too many methods in each model, and the naming of the methods can also be unified. For example, in the function library, deleting articles is named delArticle(), deleting pictures is named delPicture(), deleting users is named delUser(), and once separated by models, wouldn’t it be much neater to call them all del()?
10 Therefore, no matter the size of the project, as long as the database is used, it is always worthwhile to use MODEL.
11 A question worth discussing is that theoretically, the model is only responsible for data processing and not for output display. In several of my previous projects, the output was also placed in the view assistant of zend framework. middle. But recently, I just established a method to generate lists and directory trees directly in the model. Compared with the view assistant, it saves some time, and I haven't found any disadvantages, so I will continue until I find no problems. Do some small amount of output in the model: generate a string and return it to the relevant calling command in the controller. Thinking about it now, maybe I understood it wrong. Maybe the MODEL mentioned in the information is not responsible for output, but only requires that the page be displayed directly without echo and other methods to generate string. Maybe it is allowed in the first place.
12 3. phpunit testing framework
13 In process-oriented programming, many people write functions when needed, and then directly call the function for testing where the function needs to be called. Maybe many people, like me, suddenly discovered one day that they could not tolerate infection from various external factors, so they created a new test.php to conduct these tests. Then, as the number of tests gradually increased, test.php was changed to the test folder. It stores tests for all functions. This is the prototype of phpunit. During the entire programming process, tests take up a lot of our time, and phpunit saves these tests, saving us a lot of testing time when we modify classes and methods in the future.
14 4. Class abstraction and inheritance
15 In my code, inheritance is always happening, but there is very little abstraction. Abstraction and interface have mandatory definition requirements for us to write programs, so for programmers with few collaborators, it does not seem to make much sense. However, it is precisely because of the freedom and laxity in the programming process that we or our next programmer will often taste the bitter consequences later. I mentioned before in the opening chapter that writing this post is a process of reflection on my previous experience. So, what I want to say here is that the code I wrote before rarely had abstractions and interfaces, and in In future work, I will make up for this point to unify my naming of properties and methods.
16 5. Design Patterns
17 So far, people have summarized a total of 23 design patterns, among which I have used more registration mode, factory mode and singleton mode. Design patterns are a summary of previous object-oriented programming methods. Fortunately, when I program with the zend framework, many times I can use them directly without knowing too much about their technical details. For example, the registration class zend_register is used Registration mode and zend_form use decoration mode extensively, and technologies such as reflection API are also used reasonably. Of course, conversely, the learning difficulty of zend framework also increases. When we learn a technology, we are often not satisfied with knowing what it is, but want to know why. And these modes are obviously not covered by a manual. ZEND FRAMEWORK The reason why it is difficult to master has a lot to do with our lack of deep understanding of related knowledge such as design patterns.
18 6. Framework
19 Many people are afraid of zend framework. They would rather learn some small lightweight frameworks than contact zend framework. The reason is that it is too huge. In fact, I myself suffered a lot and wasted a lot of time learning and applying zend framework. Finally, I finally realized that the framework has changed our original programming thinking. We should use the framework as stated in the manual and introductory guide, instead of sticking to our habits and changing the framework to suit us. Otherwise, we will get half the result with half the effort and lose more than the gain. For example, I recently wrote a website with the following functions:
20 1. Display the category list and article content of all articles in the submission area of the forum;
21 2. Display the list of all articles submitted by the author’s friends ;
22 3. Automatically generate Google sitemap.
23 The functions are indeed quite few, but including the design page, it only took me two days to complete this website based on the zend framework (and it was not two full days). Therefore, although it is known as a heavyweight framework, zend framework is indeed suitable for the development of small projects. If you think zend framework is too big, you don’t have to use so many functional libraries and just use the ones you have mastered. The problem with zend framework is that it has too many functions and it is impossible to master them all in one project, so remember to do it step by step.
24 Of course, in fact, we can also use no framework at all and write object-oriented programs based on MVC method. Therefore, what I can't understand is the meaning of the existence of those lightweight frameworks. If a framework is just an architect of the MVC pattern, then, just like the confusion about smarty, why don't we just write it ourselves?
25 For example, you can write like this:
26 index.php
27
28 view plaincopy to clipboardprint?
29
30 $controller=$ _GET['controller'];
31 require_once 'controller/'.$controller.'.php';
32
33 $controller=$ _GET['controller']; ';
35
36 controller.php
37
38 view plaincopy to clipboardprint?
39
40 require_once 'some_model.php';
41 $model=new Some_Model();
42 $string=$model->getString;
43 require_once 'templates/templte.phtml';
44
45 require_once 'some_model.php';
46 $model=new Some_Model();
47 $string=$model->getString;
48 require_once 'templates/templte.phtml';
49
50 some_model.php
51 view plaincopy to clipboardprint?
52
53 class Some_Model extends PDO{
54 public function getString(){
55 $string='hello world!';
56 return $string;
57 }
58 }
59
60 class Some_Model extends PDO{
61 public function getString(){
62 $string='hello world!';
63 return $string;
64 }
65 }
66
67 template.phtml
68
69 view plaincopy to clipboardprint?
70
71 ......
72
73
74
75
76
77 ......
78
79
80
81
82
83
84 这样,MVC模式的面向对象程序就成功创建了吧?
85 当然,这个只是我随手打的例子,我买过本书叫《PHP高级程序设计:模式、框架与测试》,里面有一个自制轻量级框架的演示,比我这个要好得多。所以,我感觉那些轻量级框架,似乎没有什么存在的价值。而zend framework则不同,它的庞大类库事实上是制定了一个标准,回忆一下我们到现在,究竟换过多少类库?数据库类、分页类、缓存类、文件上传类.......在一个应用中用到,而在下一个应用中就往往因为功能要求不同、原作者停止更新甚至是不记得原来的下载地址、或是自己写的类想法有了变化重新再写一个等等理由,而更换成了另一个类。每个类看似不大学习成本不高,加到一起就不容小视了。而采用zend framework框架,哪怕只用它来做类库,总不至于那么快就淘汰掉,而且大公司出品,总能更让人用着放心。
86 所以,面向对象的程序,我以后还是会基于zend framework来写。
87 以上是我对PHP语言面向对象编程的一些看法,才疏学浅,权做抛砖引玉,请大家指正。
以上就是PHP编程方式的重新思索(下)的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!