Home >Backend Development >PHP Tutorial >Object-oriented programming in PHP: Methods for developing large-scale PHP projects (5) (Reprinted)_PHP Tutorial

Object-oriented programming in PHP: Methods for developing large-scale PHP projects (5) (Reprinted)_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 16:58:08883browse

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 It's easy to define a class to do something and 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. You want to display the product, its price, etc. You have different types of products, and the same action
might mean different things to different products. For example, showing a sound might mean playing it, but for other kinds of products it might
mean showing an image stored in a database. You can use OOP or PHP to reduce coding and improve quality:
Define a class for product, define the methods it should have (eg: display), then define classes for each type of product, starting from product
Classes come out (SoundItem class, ViewableItem class, etc.) and 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)... and then process the script , you can retrieve the type value from the database and instantiate an object named type
:
----------------------- -------------------------------------------------- -------
$obj=new $type();
$obj->action();
?>------ -------------------------------------------------- --------------------------
This is a very good feature of PHP. You can call the display of $obj without considering the type of object. method or other methods. Using the
technique, you don't need to modify the script to add a new type of object, just add 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 on any object in the main script, without if...else , and there is no need for two programmers, just happiness.
Now you agree that programming is easy, maintenance is cheap, and reusability is true?
If you manage a group of programmers, assigning work is very simple. 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.
Copy and Clone
When you create an object of $obj, you can copy the object by $obj2=$obj. The new object is a copy of $obj (not a
reference). So it has the state of $obj at that time. Sometimes, you don't want this, you just want to generate a new

like obj class

http://www.bkjia.com/PHPjc/631462.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631462.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 can...
Statement:
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