Object-oriented and facing process in PHP

xiaoxiao2021-03-06  51

Object-oriented and process dates in PHP: 2004-09-02 Robert Peake Source: PHP & More Click: 0

Object-oriented and facing process in PHP

Readers' readers in this article are for readers who want to understand the object-oriented and facing programming methods in PHP, including novices and veterans. Assume that the reader has a certain degree of familiarity for the use of PHP and classes.

Introduction

"The real genius has the ability to correctly evaluate uncertain, risky and contradictory information. - 吉尔"

When using many programming languages, you usually only use programming methods for one of the object or oriented. In PHP, you can freely choose or mix. At present, most PHP programmers use a process-oriented way, because the parsing web page itself is very "procedural" (from a label to another label). Embedding process processing code in HTML is a very direct natural approach, so PHP programmers usually use this way.

If you are just exposed to PHP, write code with the process-oriented style is likely to be your unique choice. But if you often go to the PHP forum and newsgroups, you should see an article about "objects". You may also see how to write the tutorial for object-oriented PHP code. Or you may also download some ready-made class libraries and try to instantiate the objects and use methods - Although you may not really understand why these classes can work, or why do you need to use object-oriented methods to implement .

Should the "object-oriented" style or "process" style? The two parties have supporters. "Object is inefficient" or "object is very good", there is also a hear. This article does not attempt to easily determine which of the two methods has an absolute advantage, but to find out the advantages and disadvantages of each method.

The following is an example of a process-oriented code:

The following is an object-oriented code example:

If you want to know some "object-oriented" basic knowledge, please use Google Search, there are very many exciting articles on the Internet.

Who writes the code like this?

In order to understand why this topic becomes the airline of the forum, we look at some of the comparison examples of each camp. Let's take a look at "process fanatics" and "object enthusiastic". Look at their views that it is not a little familiar.

Process is fanatical

The process of enthusiasm was criticized by computer teachers during class because this method did not use more abstract implementations. Support for process-oriented perspectives "It works!" Does not improve its programming level and grade. After graduation, they may find a job, write driver, file system, or other biased underlying programming, their attention focuses on speed and code refining.

An extreme example of "process fanatic" is to resist objects and resist abstractization. They always think about how to make the program run faster, not if someone else can read their code. They often use the program as a competition rather than team events. In addition to PHP, their favorite programming languages ​​are C and compilation. In the PHP world they may develop PECL modules to contribute high-efficiency code.

Object

Object fanators are keen to write code for object-oriented style at any time. They don't really consider whether it will affect the execution efficiency of the program. Sometimes people think they enjoy more abstract design concepts rather than real code. They are usually probably a project administrator or document writing.

Object fanators pointed out that if there is no abstract design method, we are still programming using 0 and 1. They like to use pseudo code to describe the problem. Extreme example is that the object fan is still using the object even if it is sometimes sacrificing the efficiency. In addition to PHP, their favorite language is Java and SmallTalk. In the PHP world, they may develop PEAR modules, contributing to the documentation very well, easy to maintain the code. Don't be biased and ironic

Do you know why the forum is always full of biases? Your experience experience, your attitude towards new things may be cause. As a programmer, we need to pay attention to these prejudice and learn new things in an open mindset.

Your coding tendency?

Consider what preference or tendency when you write a PHP code. Usually these preferences are more concealed. Sometimes you may have the same preference in each project. I personally prefer "elegant", but I don't want this definition to be "elegant" code, that should appear in another article. However, the theoretical preferences are not necessarily suitable for actual projects - reverse, they are often a biased.

Theoretical tendency

• Provide a complete solution with the least line of code • Consider problems at the problem level

This seems very good. But "The least" number of code lines "will measure? Do you want to explain the code note? Do we have to string every line and use a semicolon to distinguish? Big bracket? It is obvious that this idea is wrong.

Explain what is "problem hierarchy". Does this mean that every concept in our solution needs to create a class? Or need each part of each individual file to maintain a question, and create a complex file tree to correspond to the problem in reality? Is this idea - prepare a file or class for each idea!

It is clear that these generalized are extremely ridiculous. But there is a more subtle proof in reality. Whether there will be a programmer inserting a row, powerful but not commented in the team working together? This is undoubtedly a very frustrating thing for those who take over to maintain these codes. Conversely, whether your bureaucracy is the last level of programmers often "rushing straight", establishes interfaces and classes? And those interfaces and classes not only limit the programmers responsible for realization, but also limit efficiency and flexibility. Hands have no hand in the customer to request an extension program. These are all subtle proofs of the above-mentioned tendencies.

Actual tendency

When a project starts, first seek actual coding purposes and directions. What is the implementation of this project? Below is it possible to answer.

• Development fast, fast release • Run as quickly • Easy maintenance, improvement, and expansion • Publish an API

First, two directions tend to use process-oriented style, while the last two tend to use object-oriented style.

When is some way more effective?

Now let us try to evaluate the advantages of each way in reality.

Process case

A basic argument for PHP-oriented process-oriented programming advantages is: PHP is an interpretative language - this means that it is not like other languages, it will not be compiled into an executable package, but Interpretation and execute it immediately. It is a scripting language and stored in a text file (exception, if the Zend Compilation Tool is used).

Another reason to use object-oriented mode in PHP4 and lower versions is that the functionality of the object in the early version of PHP has not been well designed. Just like Rasmus once said: "That is to remember the function to increase afterwards." This means that in PHP4 and earlier versions, the efficiency of the object is a problem. But after PHP5 comes out, this situation will change.

The following two most popular PHP programs --OScommerce and phpmyadmin are used primarily for process-oriented coding. They build very quickly, running very quickly. Both naturally adopt methods embedded in HTML.

Oscommerce

Oscommerce actually uses many objects, but most of the functions are implemented through "process". I used HACK OSCommerce to add some custom features that are very practical for customers. This process is quite troubles, because many of the process code in OSCommerce, there is no template system, and is designed to be multilingual, so you need to spend a certain time. But it works, in fact it has run very well on the number of e-commerce sites. OSCommerce also provides a forum and a development framework for developing modules and plugins. Therefore, there is now a practical functional module provided by many other developers. PHPMYADMIN

PHPMYADMIN is only one class: Mimer SQL Validator class, depending on the mail_mime, net_dime, and soap in the PEAR package. This may be considered for the development of the development: utilizing the codes that can be achieved. In addition, everything is a process, HTML and PHP code are mixed together.

PHPMYADMIN is a tool I have to use almost every day to use a small amount of data table to be less complicated. Sometimes I even encourage my customers to use it as a backend management tool (of course I will limit their permissions). PHPMYADMIN performance is very great, and it is very fast. Sometimes I want to extend phpmyadmin as a backend management tool in some projects, using some of its new features such as data query statement bookmarks, can easily show my customers and editing. With the launch of each new version, phpmyadmin is increasingly practical, and the function is getting stronger.

Process summary

The above two process-oriented procedures have very good documents and code comments. The development framework provided by OSCommerce can increase maintenance and scalability. However, both do not provide an API and cannot extend the procedure to another system.

If you want to integrate OSCommerce into a billboard, you need to spend a lot of time and effort, just like extending phpMyAdmin into a backend management tool for customers. However, from the purpose of their design, it is indeed very good in their respective fields.

Object-oriented case

Object-oriented-oriented-oriented perspectives are concentrated in scalability and packaging. Just use the object-oriented way to create a document, but it can encourage you to add a document for it. And, in order to easily expand, you may write an API. PHP5 promises makes object-oriented programming more enjoyable. I joked it to the "Java 2" version in PHP because it integrates many of the characteristics in Java, like interface, object-oriented model, try-catch statement, and so on. But even in PHP4 that supports the face-to-object support, there is still a lot of excellent object-oriented applications.

Smarty

Smarty is used to build a complex form and a template-based site. Recently, I wrote an online test system that can be completely changed "Skin" - can change the appearance interface and style of the entire site without changing the code and feature of any underlying. In order to make the designer can easily design a new interface, I have designed a custom label library as an extension of the Smarty tag library. Can be simply inserted like this:

[Navigation Horizontal Separated BY "|"]

There is a separate navigation at the top of a page. Because Smarty has provided a very powerful mechanism to expose data contained in the variable, this is a simple process of mapping more complex smarty tags to SKIN tags. For more information about this: http://simplequiz.peakepro.com/

Since SMARTY is encapsulated into a class, and its method has a detailed document, it makes it incredibly easy to expand the process of using the template. At the same time, by mandatoryly deliberately delivering the variable you want to use to the Smarty Template, Smarty also provides a protective layer for PHP environment variables. This method helps to establish a safe and reliable work relationship between the Smarty template designer and programmer. FPDF

FPDF is a very good tool. If you have been changed by the changed PDFLIB's API, or if you are not willing to pay for a commercial solution; or you cannot use extended modules due to shared host - Consider using this free, pure PHP built PDF generation tool.

This class has a good document, including many good examples to explain how to lay out and images in PDF. The same online learning site mentioned above I use FPDF to dynamically generate PDF files, using TRUE TYPE fonts and 300DPI accuracy images. Executive FPDF classes in PHP and makes PDF operations do not cost too much extra time, because PDF itself may take a few minutes to download. In fact, the time spent in dynamically generating and transmitting a PDF is not more time to transmit a static PDF file when using a slow network connection. This is relatively. And, since the FPDF is based on class, he can be expanded. In fact, some types of methods have existed but have not fully implemented, only as a framework, which can provide a wizard for you to create your own content (such as custom header elements) in subclasses.

FPDF

FPDF is a very good tool. If you have been changed by the changed PDFLIB's API, or if you are not willing to pay for a commercial solution; or you cannot use extended modules due to shared host - Consider using this free, pure PHP built PDF generation tool.

This class has a good document, including many good examples to explain how to lay out and images in PDF. The same online learning site mentioned above I use FPDF to dynamically generate PDF files, using TRUE TYPE fonts and 300DPI accuracy images. Executive FPDF classes in PHP and makes PDF operations do not cost too much extra time, because PDF itself may take a few minutes to download. In fact, the time spent in dynamically generating and transmitting a PDF is not more time to transmit a static PDF file when using a slow network connection. This is relatively. And, since the FPDF is based on class, he can be expanded. In fact, some types of methods have existed but have not fully implemented, only as a framework, which can provide a wizard for you to create your own content (such as custom header elements) in subclasses.

Object-oriented summary

Both Smarty and FPDF provide APIs with a good document to extend the primary class. This illustrates the necessity of internal tissue methods and data in the class - sometimes completed by functions and global variables, but it is not easy to expand. Also, use objects help with the style of tracking and keeping PDF or HTML documents, you can publish the same data in different formats. SMARTY and FPDF are excellent examples of using objects to establish flexible practical class libraries.

Why is the two ways necessary?

Going to us on our enthusiastic programmers, we start praising them:

• We appreciate the practicality and scalability of Smarty and FPDF • We enjoy the running speed and good performance of OSCommerce and phpmyadmin

This appreciation also includes some basic development of PHP. PECL and PEAR have received a lot of praise and criticism. I think these two projects provide a good example to clarify the differences or object-oriented programming.

PECL provides PHP expansion libraries, develops, focusing on speed and concise refinement with C and-oriented procedures. Typically, these are transplanted from the existing LGPL software, many of which have been added to PHP. After all, PHP is written with C.

PEAR contributes a lot of interesting classes such as building an Excel table or changing DNS records. Using the PEAR class library can save you a lot of time, and you can even make you develop in situations who are not familiar with PHP - "I don't understand but it can be used!" to sum up

I hope this article can deepen your understanding of two programming methods, and more importantly - encouraging you to explore in more detail. I hope that you will have your own ideas and test your project development tendency in actual development, summarize more practical cases, and don't write some comments for this article.

In short, each way has its advantages, entanglement is not as good as left to write some actual code!

About author

Robert Peake gets a computer program at Berkeley before the Poetry Professional Degree. Recently resigned from the IT manager's position, consider spend more time to spend more time and PHP. He and his wife Valerie live in Los Angeles. You can contact him by email: Robert At peakepro.com

转载请注明原文地址:https://www.9cbs.com/read-116870.html

New Post(0)