Using classes for data storage
A great thing about PHP and OOP is that you can easily define a class to manipulate something, and you can call the class whenever you want to use it. If you have an HTML form, the user can select a product by selecting the Product ID number. In the database has the product information, you want to display the product, displays its price and so on. You have different types of products, and the same action may have different meanings for different products. For example, displaying a sound might mean playing it, but for other kinds of products it might mean displaying a picture in a database. You can use OOP or PHP to reduce coding and improve quality:
Define a product's class, define the methods it should have (for example: Display), and then define the classes for each type of product, from the Product class Pie (Sounditem class, Viewableitem class, etc.), and cover the methods in the product class so that they act as you think.
A typical product table may have (ID, type, price, description, etc. fields) based on the Type field named for each product in the database. Then in the processing script, you can take the type value out of the database and instantiate an object named type:
?>--------------------------------------------------------------------------------
This is a very good feature of PHP, you can call the $obj display method or other method without considering the type of object. Use
This technique, you don't need to modify the script to add a new type of object, just add a class that handles it.
This is a powerful feature, as long as you define the method without considering the types of all the objects, implement them in different ways in different classes, and then use them in the main script for any object, no if...else, no two programmers, just be happy.
Now you agree that programming is easy, maintenance is cheap, reusable is true?
If you manage a group of programmers, assigning work is simple, and each person may be responsible for one type of object and the class that handles it.
You can internationalize through this technology, apply the appropriate classes to the language fields that the user chooses, and so on.
Copy and Clone
When you create a $obj object, you can copy the object by $obj2= $obj, and the new object is a copy of $obj (not a
Reference), so it has a state of $obj at that time. Sometimes you don't want to, you just want to generate a new one like the Obj class
object, you can invoke the class's constructor by using the new statement. It can also be done in PHP by serialization, and a base class, but the
Some other classes are derived from the base class.
Enter the danger zone.
When you serialize an object, you get a string of some form, and if you're interested, you can tune it, where the string has
The name of the class (great!) ), you can take it out like this:
?>--------------------------------------------------------------------------------
So suppose you create a "universe" class and force all classes to be extended from universe, you can universe
To define a clone in the following way:
--------------------------------------------------------------------------------
<?php
?>--------------------------------------------------------------------------------
What you get is a new something class object that uses the same object that the constructor creates by using the new method. I don't know if this works for you, but the Universe class can know the name of a derived class is a good experience. Imagination is the only limit.
Note: I use PHP4, I write some things under PHP3 may not work.
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.