PHP Object-oriented and PHP process-oriented pros and cons _php tutorial

Source: Internet
Author: User
Tags php language pear oscommerce phpmyadmin smarty template
Object-oriented and process-oriented can only be programmed in one of many programming languages, but the PHP language differs from other programming languages in that we are free to choose or toIf you're new to PHP, writing code with PHP's process-oriented style is probably your only option. But if you go to PHP forums and newsgroups Often, you should see articles about "objects". You may also have seen tutorials on how to write object-oriented PHP code. Or you might have downloaded some out-of-the-box libraries and tried to instantiate objects and use class methods-although you might not really understand why these classes work, or why you need to use the PHP object-oriented approach to implement functionality.

Should you use the "Object-oriented" style or the "process-oriented" style? Each side has its supporters. Such arguments as "objects are inefficient" or "objects are very good" are also heard. This article does not attempt to easily determine which of the two methods has the absolute advantage, but to find out the pros and cons of each method.

Here's a sample code for procedural styles in PHP:

 
  
  
  1. Php
  2. Print "Hello,world.";
  3. ?>
Here is an example of PHP object-oriented style code:

 
  
  
  1. Php
  2. classhelloworld{
  3. Functionmyprint () {
  4. Print "Hello,world.";
  5. }
  6. }
  7. $ Myhelloworld = Newhelloworld ();
  8. $myHelloWorld- > myprint ();
  9. ?>

If you want to learn some basics of "object-oriented", use Google search, which has a lot of wonderful articles on the web.

who writes code like this?

To understand why this topic has become a flashpoint for the forum water fights, let's look at some of the more extreme examples of each faction. Let's look at "Process Mania" and "Object Mania". See if their point of view sounds a little familiar.

Process Mania

The process mania was criticized by computer teachers in class because it did not use a more abstract approach to implementation. and support PHP for the process-oriented perspective "it can work!" "does not improve its programming level and grade. After graduation they may find a job, write drivers, file systems or other bias-biased programming, focusing their attention on speed and the refinement of the code.

The extreme example of "process Mania" is resisting the object and resisting abstraction. They are always thinking about how to make the program run faster and don't care if someone can read their code. They often regard programming as a contest rather than as a team activity. In addition to PHP, their favorite programming language is C and assembler. In the PHP world they may develop PECL modules and contribute efficient code.

Object Mania

Object fanatics are keen to write code at any time using the object-oriented style of PHP. They did not really consider whether this approach would affect the execution efficiency of the program. Sometimes it is felt that they enjoy abstract design concepts rather than realistic code. They are often likely to be project managers or document writers.

Object enthusiasts point out that without abstract design methods we are still programming with 0 and 1. They like to use pseudo code to describe the problem. Extreme examples are object fanatics who even know that sometimes sacrifice efficiency still uses objects. In addition to PHP, their favorite language is Java and Smalltalk. In the PHP world, they may develop pear modules that contribute to documenting very good, easy-to-maintain code.

don't be extreme and sarcastic .

Do you know why the forums are always full of prejudices? Your experience, your attitude to new things may be the reason. As programmers, we need to constantly pay attention to these prejudices and learn new things with an open mind.

What's your coding tendency?

Consider what preferences or tendencies you have when writing PHP code. Usually these preferences are somewhat obscure. Sometimes you may have the same preference in every project. I personally tend to be "classy", but I don't want to define what is "elegant" code here, which should appear in another article. However, the theoretical preferences are not necessarily suitable for actual projects-they are often a bias, on the contrary.

The tendency of the theory

• Provide a complete solution with a minimum number of lines of code

• Consider issues at the problem level

It sounds like a good idea. But how do you measure the "minimum number of lines of code"? Do you want to include the code comments? Do we have to string up each line and just use semicolons to differentiate it? What about curly braces? It is obvious that the idea is wrong.

Explain again what is the "problem level". Does this mean that each concept in our scenario needs to have a class created? Or do you need to keep each part of the problem in each separate file and build a complex file tree that corresponds to a real-world problem? That's the idea--Prepare a file or class for each idea!

It is obvious that these generalizations have become ridiculous after the extremes. But there are more subtle proofs of reality. Is it common for programmers to insert a line of complex, powerful, but no-commented code when working in a team? This is a very frustrating thing for those who take over the maintenance of the code. Conversely, is your bureaucrat's self-righteous upper-level programmer often "rampage" to build interfaces and classes? Those interfaces and classes not only restrict the programmer responsible for implementation, but also limit efficiency and flexibility, causing customers to be overwhelmed by the need to extend the program. These are subtle proofs of the various tendencies above.

Practical Tendency

At the beginning of a project, the first thing to look for is the actual coding purpose and direction. What is the goal of this project? Here is probably the answer.

• Fast development, Fast release

• Run as fast as you can

• Easy to maintain, improve and extend

• Publish an API

The first to second direction tends to use a procedural style, while the last two tend to use the object-oriented style of PHP.

When is a way more effective?

Now let's try to evaluate the advantages of each approach in reality.

PHP Process-oriented case

A fundamental argument about PHP's procedural-oriented programming advantage is that PHP is an explanatory language-which means that, unlike other languages, it is not compiled into an executable package, but is interpreted and executed immediately. It is a scripting language and is stored in a text file (except if the Zend Compilation tool is used).

Another objection to the use of object-oriented encoding in PHP4 and earlier is that the functionality of the object was not well designed in earlier versions of PHP. As Rasmus once said, "It is only after hindsight that we think of the added function." This means that the efficiency of the object is a problem in PHP4 and earlier versions. But after PHP5 out, this situation will change.

The following two most popular PHP programs--oscommerce and phpMyAdmin. Mainly use process-oriented encoding. They build up quickly and run quickly. Both are naturally embedded in HTML methods.

oscommerce

OsCommerce actually uses a lot of objects, but most of the functionality is done through "process." I have hack oscommerce to add some custom features that are very useful to customers. This process is quite troublesome, because many of the process code in oscommerce, not using a templated system, and designed to be multilingual, it takes a while to get started. But it can work, in fact it has run well on a large number of e-commerce sites. OsCommerce also provides a forum and a development framework for developing modules and plugins. As a result, there are many useful functional modules available from other developers.

PhpMyAdmin

phpMyAdmin directly uses only one class: The Mimersqlvalidator class, which relies on mail_mime,net_dime and soap in the pear package. This may be due to the ease of development: using ready-made code that can be used to achieve the purpose. In addition, everything is process-oriented, and HTML and PHP code are mixed together.

phpMyAdmin is a tool that I use almost every day to do less complex processing of a small number of data tables. Sometimes I even encourage my clients to use it as a back-end management tool (I will, of course, limit their permissions). phpMyAdmin's performance was great 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 that can be easily presented to my customers and editors. With the launch of each new release, phpMyAdmin is becoming more and more functional and more powerful. Software Development Network

php-oriented process summary

The above two programs with process-oriented styles have very good documentation and code comments. The development framework provided by OsCommerce can increase maintainability and extensibility. But neither provides the API, and cannot extend the program to another system.

If you want to integrate oscommerce into a billing program, you'll need to spend a lot of time and effort, like expanding phpmyadmin into a back-end management tool for customers to use. But from the point of view of their design, they do perform well in their respective fields.

PHP Object-oriented case

The idea of supporting object-oriented styles is focused on extensibility and encapsulation. Writing code in an object-oriented way does not produce documentation for your code, but it encourages you to add documents to it. And, to be easy to scale, you might write an API. PHP5 promises to make object-oriented programming more enjoyable. I jokingly call it the "JAVA2" version in PHP because it consolidates many of the features in Java, like interfaces, object-oriented models, try-catch statements, and so on. But even in the object-oriented support not

PHP4, there are still many excellent object-oriented applications.

Smarty

Smarty is used to build sites with complex forms and template-based. Recently, I wrote an online exam system that completely changed the skin-without changing any of the underlying code and features to completely change the look and feel 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 simply insert them like this:

[Navigationhorizontalseparatedby "|"]

There is a separate navigation at the top of a page. Because Smarty has provided a very powerful mechanism to represent the data contained in variables, this is a simple process of mapping a more complex smarty label to a skin label.

Because Smarty is packaged into a class, and its methods have very detailed documentation, the process of using templates becomes incredibly easy to scale. At the same time, Smarty also provides a layer of protection for PHP's environment variables by forcing the method to explicitly pass only the variables you want to use to the Smarty template. This approach helps to establish a secure and reliable working relationship between Smarty template designers and programmers.

FPDF

Fpdf is a very good tool. If you're confused by the Pdflib API that you've changed, or don't want to pay for a commercial solution, or you can't use an extension because of the limitations of the shared host-consider using this free, pure PHP-built PDF generation tool.

This class has good documentation, including many good examples of how to lay out text and pictures in a PDF. In the same online learning site mentioned above I use Fpdf to dynamically generate PDF files using TrueType fonts and 300dpi precision images. Instantiating the Fpdf class and doing a PDF operation in PHP does not take much extra time, as the PDF itself may take several minutes to download. In fact, the time it takes to dynamically generate and deliver a PDF is no more than the time it takes to transfer a static PDF file using a slow network connection. It's all relative. Also, because Fpdf is class-based, he can be extended. In fact, some class methods exist but are not fully implemented as a framework, which can provide wizards for you to build your own content in subclasses, such as custom kinsoku elements.

PHP Object-oriented summary

Both Smarty and Fpdf provide APIs with good documentation to extend the main class. This illustrates the need to organize methods and data within a class-sometimes the same functionality can be done with functions and global variables, but this is not easy to scale. Also, using objects is very helpful in tracking and maintaining the style of a PDF or HTML document, and you can publish the same data in a different format. Smarty and fpdf are excellent examples of using objects to create flexible and practical class libraries.

Why are both of these methods required?

Back to our passionate handlers, we begin to praise them:

• We appreciate the practicality and extensibility of smarty and Fpdf

• We appreciate the operating 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 of the differences between process-oriented and object-oriented programming in PHP.

PECL provides an extended library of PHP, developed in C and in a process-oriented way, with a focus on speed and concise refinement. Typically, these are migrated from existing LGPL software, many of which have been added to PHP. After all, PHP is written in C.

Pear has contributed a lot of interesting classes such as creating Excel tables or changing DNS records. Using the Pear class Library can save you a lot of time and even allow you to develop with less familiarity with PHP-"I don't understand but it can work!" ”。

Summary

Hopefully this article will deepen your understanding of both the PHP object-oriented and PHP-oriented approach to programming and, more importantly, encourage you to explore in more specific detail. I hope that you will have your own ideas, and in the actual development test your project development tendencies, summed up more practical cases, do not Sik Sik Yuen write some comments on this article.

In short, each way has its advantage side, entangled in the argument is better off to write some actual code!


http://www.bkjia.com/PHPjc/446406.html www.bkjia.com true http://www.bkjia.com/PHPjc/446406.html techarticle object-oriented and process-oriented can only be programmed in one of many programming languages, but the PHP language differs from other programming languages in that we are free to choose ...

  • 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.