PHP object-oriented and PHP process-oriented advantages and disadvantages evaluation

Source: Internet
Author: User
Tags oscommerce
(Switch) what are the advantages and disadvantages of PHP object-oriented and PHP process-oriented comparison ?? Object-oriented and process-oriented programming can only be used in many programming languages. However, PHP is different from other programming languages, that is, we can freely choose or mix PHP object-oriented and PHP object-oriented processes. Currently, most PHP programmers use process-oriented methods, because parsing Web pages is very "procedural" (from one tag to another ). Comparison of advantages and disadvantages of HTM (to) PHP object-oriented and PHP process-oriented

?? Object-oriented and process-oriented programming can only be used in many programming languages. However, PHP is different from other programming languages, that is, we can freely choose or mix PHP object-oriented and PHP object-oriented processes. Currently, most PHP programmers use process-oriented methods, because parsing Web pages is very "procedural" (from one tag to another ). It is natural to process code during the embedded process in HTML, so PHP programmers usually use this method.

?

If you are new to PHP, writing code in PHP's process-oriented style may be your only choice. However, if you often go to PHP forums and newsgroups, you should see articles about "objects. You may also see tutorials on how to write object-oriented PHP code. Or you may have downloaded some ready-made class libraries and tried to instantiate the objects and use the class methods-although you may not really understand why these classes work, or why you need to use the PHP object-oriented method to implement the function.

?

Should we use the "object-oriented" style or the "process-oriented" style? Both parties have their own supporters. Discussions like "objects are inefficient" or "objects are great" are also heard. This article does not try to determine which of the two methods has absolute advantages, but to find out the advantages and disadvantages of each method.

?

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

Who writes code like this?

To understand why this topic has become a fuse for the forum, let's look at some extreme examples of each camp. Let's take a look at "process fanatics" and "object fanatics ". Let's see if they seem familiar.

Process craze

The process craze was criticized by computer teachers in class because it did not use a more abstract implementation method. The support for PHP for process-oriented views "it can work !" It cannot improve the programming level and grade. After graduation, they may find a job, write drivers, file systems, or other underlying-oriented programming, with their focus on speed and code refining.

The extreme example of "process fanaticism" is to resist objects and abstract them. They are always thinking about how to make the program run faster, regardless of whether others can understand their code. They often regard programming as a competition rather than a team activity. Apart from PHP, their favorite programming languages are C and assembly. In the PHP world, they may develop the PECL module and contribute efficient code.

Fanatic object

Object enthusiasts are keen to use PHP object-oriented style to write code at any time. They have not really considered whether using this method will affect the execution efficiency of the program. Sometimes people feel that they prefer abstract design concepts rather than real code. They are usually project managers or document writers.

The object enthusiast points out that if there is no abstract design method, we are still Programming with 0 and 1. They like to use pseudo-code to describe the problem. An extreme example is that object enthusiasts still use objects even if they know they sometimes sacrifice efficiency. Apart from PHP, their favorite languages are Java and Smalltalk. In the PHP world, they may develop PEAR modules and contribute well-documented and easy-to-maintain code.

Don't be extreme or ironic

Do you know why forums are always filled with various prejudices? Your experience and attitude towards new things may be the cause. As programmers, we often need to pay attention to these prejudices and learn new things with an open mind.

Your coding preferences?

Consider your preferences or preferences when writing PHP code. These preferences are usually relatively obscure. Sometimes you may have the same preferences in each project. I personally tend to be "elegant", but I don't want to define how the code is "elegant" here, it should appear in another article. However, theoretical preferences are not necessarily suitable for actual projects-on the contrary, they are often biased.

Theoretical tendency

? Provide a complete solution with the minimum number of lines of code

? Consider the problem at the problem level

It sounds good. But how can we measure the "minimum number of lines of code? Do you want to include code comments? Do we need to concatenate each line and only use semicolons to differentiate it? What about braces? Obviously, this idea is wrong.

Next, let's explain what is "problem level ". Does this mean that a class needs to be created for every concept in our solution? Or do you need to maintain each part of the problem in each independent file and create a complex file tree to correspond to the problem in reality? That's the idea: prepare a file or class for each idea!

Obviously, these summaries become ridiculous after the ultimate. But there are more subtle proofs in reality. Do programmers often insert a complex, powerful, but unannotated line of code when working in a team? This is undoubtedly very frustrating for those who take over and maintain the code. On the contrary, is it true that your bureaucratic self-righteous top-level programmers often create interfaces and classes in a "straight forward" manner? Those interfaces and classes not only restrict the programmers responsible for implementation, but also restrict the efficiency and flexibility, resulting in the customer's inability to request the extended program. These are the subtle proofs of the above tendencies.

Actual tendency

At the beginning of a project, you must first seek the actual encoding purpose and direction. What is the goal of this project? Below is the answer.

1. fast development and fast release

2. run as quickly as possible

3. easy to maintain, improve, and expand

4. publish an API

The first and second directions tend to adopt procedural styles, while the last two tend to use PHP object-oriented styles.

When is some method more effective?

Now let's evaluate the advantages of each method in reality.

PHP process-oriented case

A fundamental argument for PHP's advantages in process-oriented programming is that PHP is an explanatory language-which means, unlike other languages, instead of being compiled into an executable package, it is interpreted and executed immediately. It is a scripting language and is stored in text files (except if Zend compilation tool is used ).

Another reason against using object-oriented encoding in PHP4 and earlier versions is that in earlier versions of PHP, object functions were not well designed. As Rasmus once said: "It was only after the event that we remembered the features to be added ". This means that in PHP4 and earlier versions, object efficiency is a problem. But after PHP5 is released, this situation will change.

The following two most popular PHP programs-OsCommerce and PhpMyAdmin-mainly use process-oriented encoding. They are quickly built and run quickly. Both of them naturally adopt the method of embedding HTML.

OsCommerce

OsCommerce actually uses many objects, but most of the functions are implemented through the "process. I used to hack OsCommerce to add some user-defined functions that are very useful to customers. This process is quite troublesome because many process codes in OsCommerce do not use templated systems and are designed to be multilingual, so it takes some time to get started. But it can work. In fact, it is already running well on a large number of e-commerce sites. OsCommerce also provides a forum and a development framework for developing modules and plug-ins. Therefore, many functional modules provided by other developers are available.

PhpMyAdmin

PhpMyAdmin directly uses only one class: MimerSQLValidator class, depending on Mail_Mime, Net_DIME, and SOAP in the PEAR package. This may take into account the convenience of development: the use of off-the-shelf code that can achieve the purpose. In addition, everything is process-oriented, and HTML and PHP code are also mixed.

PhpMyAdmin is a tool that I use almost every day. it is used to process a small number of data tables less complex. Sometimes I even encourage my customers to use it as a backend management tool (of course I will limit their permissions ). PhpMyAdmin is very good and 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 be easily displayed to my customers and edited. With the release of each new version, PhpMyAdmin becomes more and more practical and powerful. Software Development Network

PHP process-oriented summary

The above two process-oriented programs have excellent documentation and code comments. The development framework provided by OsCommerce can increase maintainability and scalability. However, neither of them provides APIs and cannot be extended to another system.

If you want to integrate OsCommerce into a Bill application, it takes a lot of time and effort, just like extending PhpMyAdmin into a backend management tool for customers. However, from the perspective of their design purpose, they did well in their respective fields.

PHP object-oriented case study

All views that support object-oriented styles are focused on scalability and encapsulation. Writing code in an object-oriented way alone does not generate documents for your code, but it encourages you to add documents for it. In addition, you may write an API for ease of expansion. PHP5 promises to make object-oriented programming more enjoyable. I jokingly called it the "Java2" version in PHP because it integrates many features in Java, such as interfaces, object-oriented models, and try-catch statements. However

There are still many outstanding object-oriented applications in force PHP4.

Smarty

Smarty is used to build websites with complex forms and templates. Recently, I wrote an online examination system that can completely change the "skin". without changing any underlying code and functions, I can completely change the appearance and style of the entire site. To make it easy for designers to design new interfaces, I designed a custom tag library as an extension of the Smarty tag library. You can insert it as follows:

[Navigationhorizontalseparatedby "|"]

Navigation is separated at the top of a page. Because Smarty already provides a very powerful mechanism to express the data contained in variables, it is a simple process of ing complicated Smarty labels to skin labels.

Because Smarty is encapsulated into a class and its methods have detailed documentation, the process of using templates becomes incredibly easy to expand. At the same time, by forcibly passing only the variables you want to use to the Smarty Template method, Smarty also provides a protective layer for PHP environment variables. This method helps to establish a secure and reliable working relationship between the Smarty template designer and the programmer.

FPDF

FPDF is an excellent tool. If you are confused by the modified pdflib API, or do not want to pay for commercial solutions, or because of restrictions on shared hosts, unable to use the extension module-please consider using this free, pure PHP-built PDF generation tool.

This class has good documentation, including many good examples to illustrate how to layout text and images in PDF. On the same online learning site mentioned above, I used FPDF to dynamically generate PDF files, using truetype fonts and DPI precision images. It does not take much time to instantiate the FPDF class in PHP and perform PDF operations, because the PDF itself may take several minutes to download. In fact, it takes more time to dynamically generate and transmit a PDF file than to transmit a static PDF file using a slow network connection. This is relative. In addition, because FPDF is class-based, it can be extended. In fact, although some class methods exist, they are not fully implemented yet and only serve as a framework. this provides a wizard for you to create your own content (such as custom header and tail elements) in the subclass.

PHP object-oriented summary

Both Smarty and FPDF provide APIs with good documentation to expand the main class. This illustrates the necessity of organizing methods and data within the class-sometimes the same function can be done using functions and global variables, but this is not easy to expand. In addition, using objects is very helpful for tracking and maintaining the style of PDF or HTML documents. you can publish the same data in different formats. Both Smarty and FPDF are excellent examples of using objects to build flexible and practical class libraries.

Why are both required?

Back to our enthusiastic programmers, we began to praise them:

1. we appreciate the practicality and scalability of Smarty and FPDF.

2. we appreciate the running speed and good performance of osCommerce and phpMyAdmin.

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

PECl provides a PHP Extension Library, which is developed in C and process-oriented mode, focusing on speed and conciseness. These are usually transplanted from the existing LGPL software, and many of the interesting features have been added to PHP. After all, PHP is written in C.

PEAR has contributed many interesting classes, such as creating an Excel table or changing DNS records. Using the PEAR class library can save you a lot of time, or even allow you to develop without getting familiar with PHP-"I don't understand, but it can be used !".

Summary

I hope this article will deepen your understanding of PHP object-oriented and PHP process-oriented programming methods and, more importantly, encourage you to explore in more specific details. I hope you will have your own ideas, test your project development preferences in actual development, summarize more practical cases, and write some comments on this article.

In short, each method has its own advantages. it is better to leave the debate to write some actual code!

Related Article

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.