Method 5 of PHP object-oriented programming for large-scale PHP Project Development

Source: Internet
Author: User
Method 5 of PHP object-oriented programming for large-scale PHP project development is Reprinted. PHP object-oriented programming: method for developing large PHP projects (5) Author: LuisArgerich translator: limodou using classes to store data is a good thing for PHP and OOP, your PHP object-oriented programming: method for developing large PHP projects (5) Author: Luis Argerich translator: limodou using classes to store data is a very good thing for PHP and OOP, you can easily define a class to operate a thing, and call the corresponding class whenever you want to use it. Suppose you have an HTML form. you can select a product by selecting the product ID. There is product information in the data warehouse. you want to display the product and its price. You have different types of products, and the same action may have different meanings for different products. For example, displaying a sound may mean playing it, but for other products, it may mean displaying an image in the database. You can use OOP or PHP to reduce coding and improve quality: define the class of a product and define its proper method (for example, display ), then define the classes of each type of product, and dispatch them from the product class (SoundItem class, ViewableItem class, and so on) to cover the methods in the product class, let them act as you think. Name the class according to the type field of each product in the database. a typical product table may have (id, type, price, description, and other fields )... then, in the processing script, you can extract the type value from the database and instantiate an object named type :-------------------------------------------------------------------------------- Action ();?> ------------------------------------------------------------------------------ This is a very good feature of PHP. you do not need to consider the object type, call the $ obj display method or other methods. With this technology, you do not need to modify the script to add a new type of object, but add a class to process it. This function is very powerful, as long as you define methods, instead of considering the types of all objects, implement them in different classes according to different methods, and then use them for any objects in the main script, no if... else, there is no need for two programmers, just happy. Now you agree that programming is easy, maintenance is cheap, and reusable is true? If you manage a group of programmers, it is very easy to assign jobs. each person may be responsible for a type of object and the class that processes it. You can use this technology to achieve internationalization and apply the corresponding classes based on the language fields selected by the user. Copy and clone when you create a $ obj object, you can use $ obj2 = $ obj to copy the object, the new object is a copy of $ obj (not a reference), so it has the status of $ obj at that time. Sometimes, you just want to generate a new object like the obj class. you can use the new statement to call the class constructor. PHP can also be implemented through serialization and a base class, but all other classes must be derived from the base class. Enter the dangerous area. when you serialize an object, you will get a string of some format. if you are interested, you can call it. among them, there is a class name in the string (great !), You can take it out, like :-------------------------------------------------------------------------------- If you create a "Universe" class and force all classes to be extended from universe, you can define a clone method in universe, as shown below :-------------------------------------------------------------------------------- Clone ();?> ------------------------------------------------------------------------------ What you get is a new Something Class object, which is the same as the object created by calling the constructor using the new method. I don't know if this is useful to you, but the Universe class can know that the name of a derived class is a good experience. Imagination is the only restriction. Note: I use PHP4, and some of the things I write may not work under PHP3.

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.