Method 5 of developing large-scale PHP projects using PHP object-oriented programming_PHP tutorial

WBOY
Release: 2016-07-13 17:23:07
Original
828 people have browsed it

Object-oriented programming in PHP: Methods for developing large-scale PHP projects (5) Author: Luis Argerich Translator: limodou Using classes for data storage A very good thing about PHP and OOP is that you can easily define a class to operate Something, and you can call the corresponding class whenever you want to use it. Suppose you have an HTML form that allows the user to select a product by selecting the product ID number. There is product information in the database, and you want to display the product, its price, etc. You have different types of products, and the same action may mean different things to different products. For example, displaying a sound might mean playing it, but for other kinds of products it might mean displaying an image stored in a database. You can use OOP or PHP to reduce coding and improve quality: Define a class for a product, define the methods it should have (e.g. display), and then define classes for each type of product, deriving from the product class ( SoundItem class, ViewableItem class, etc.), override the methods in the product class to make them behave as you want. Name the class according to the type field of each product in the database. A typical product table may have (id, type, price, description, etc. fields)... Then in the processing script, you can retrieve it from the database Get the type value from , and then instantiate an object named type: ---------------------------------- --------------------------------------------- action( ); ?>------------------------------------------------- ---------------------------------- This is a very good feature of PHP. You don't need to consider objects. Type, call the display method or other methods of $obj. Using this technique, you don't need to modify the script to add a new type of object, just a class to handle it. This function is very powerful. Just define methods without considering the types of all objects, implement them in different classes in different methods, and then use them in the main script for any object, no if...else, no Requires two programmers, only happy. Now you agree that programming is easy, maintenance is cheap, and reusability is true? If you manage a group of programmers, it is very simple to distribute the work. Each person may be responsible for a type of object and the class that handles it. Internationalization can be achieved through this technology, just apply the corresponding class according to the language field selected by the user, and so on. Copying and Cloning When you create an object of $obj, you can copy the object by $obj2=$obj. The new object is a copy (not a reference) of $obj, so it has the state of $obj at that time. Sometimes, you don't want to do this. You just want to generate a new object like the obj class. You can call the constructor of the class by using the new statement. This can also be achieved in PHP through serialization and a base class, but all other classes must be derived from the base class. Entering the danger zone When you serialize an object, you get a string in some format, and you can interrogate it if you're interested, where, in the string, there is the name of the class (great!) and you can put Take it out like: -------------------------------------------------- ------------------------------------ ---------- -------------------------------------------------- ------------------- So assuming you create a "Universe" class and force all classes to extend from universe, you can define a clone in universe The method is as follows: ------------------------------------------------ ---------------------------------- clone(); ?>------ -------------------------------------------------- -------------------------- What you get is a new Something class object, which is the same as the object created by using the new method and calling the constructor. Same. I don't know if this will be useful to you, but it's a good rule of thumb that the universe class knows the name of the derived class. Imagination is the only limit. Note: I am using PHP4, and some of the things I wrote may not work under PHP3. Reprinted from PHPBuilder.com

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532250.htmlTechArticleObject-oriented programming in PHP: Methods for developing large-scale PHP projects (5) Author: Luis Argerich Translator: limodou Use Classes for Data Storage One very nice thing about PHP and OOP is that you...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template