PHP object-oriented programming: method for developing large PHP projects (5)

Source: Internet
Author: User
A good thing for php and OOP to store data using classes is that you can easily define a class to operate on something, you can call the corresponding classes whenever you want to use them. Suppose you have an HTML form. you can select a product by selecting the product ID. There is product information in the database. 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. Use Classes for data storage
A good thing for php and OOP is that you can easily define a class to operate on something 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 a product class, define its expected method (for example, display), and then define the class of each type of product, which is sent from the product class (SoundItem class, viewableItem class, etc.), overwrite the methods in the product class, and make them act 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, and other fields )... in the processing 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 $ obj display method or other methods without considering the object type. Use
In 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
So it has the status of $ obj at that time. Sometimes, you just want to generate a new one 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,
Some other classes must be derived from the base class.



Enter Dangerous area
When you serialize an object, you will get a string of some format. if you are interested, you can call it.
Class name (great !), You can take it out, such:

--------------------------------------------------------------------------------

$ Herring = serialize ($ obj );
$ Vec = explode (':', $ herring );
$ Nam = str_replace ("\" ",'', $ vec [2]);

?> --------------------------------------------------------------------------------
So if you create a "Universe" class and force all classes to be extended from universe, you can
To define a clone method, as follows:
--------------------------------------------------------------------------------

Class Universe {
Function clone (){
$ Herring = serialize ($ this );
$ Vec = explode (':', $ herring );
$ Nam = str_replace ("\" ",'', $ vec [2]);
$ Ret = new $ nam;
Return $ ret;
}
}

// Then
$ Obj = new Something ();

// Extend from Universe
$ Other = $ obj-> 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.


Previous Page

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.