Object-oriented Programming in PHP: How to develop a large PHP project (v) (reprint) _php Tutorial

Source: Internet
Author: User
Object-oriented Programming in PHP: A way to develop a large PHP project (v)
Author: Luis Argerich Translator: Limodou
Using classes for data storage
A great thing about PHP and OOP is that you can easily define a class to manipulate something, and whenever you want to
You can call the appropriate class when you use it. Suppose you have an HTML form that allows users to select a product by selecting the Product ID number. In the number
According to the product information in the library, you want to display the product, show 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 may mean playing it, but for other kinds of products it may
means to display a picture in the database that exists. You can use OOP or PHP to reduce coding and improve quality:
Define a product class, define the method it should have (for example: Display), and then define the class for each type of product, from the product
Classes are sent out (Sounditem class, Viewableitem class, etc.), covering the methods in the product class so that they act as you think.
A typical product table may have (ID, type, price, and so on), depending on the type field of each product in the database.
Description, etc. field) ... Then in the processing script, you can remove the type value from the database and instantiate a name of type
The object:
--------------------------------------------------------------------------------
$obj =new $type ();
$obj->action ();
?>--------------------------------------------------------------------------------
This is a very good feature of PHP, you can not consider the type of object, call $obj display method or other methods. 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, and then use them in the main script for any object, no if...else, no two programmers, only happy.
Now do you agree that programming is easy, maintenance is cheap, reusable is true?
If you manage a group of programmers, assigning jobs is easy, and everyone can be responsible for one type of object and the class that handles it.
Internationalization can be achieved through this technology, according to the user's chosen language field to apply the corresponding class can be, and so on.
Copy and Clone
When you create a $obj object, you can copy the object through the $obj2= $obj, and the new object is a copy of the $obj (not a
A reference), so it has the $obj state at the time. Sometimes, you don't want to do this, you just want to create a new one like the Obj class

http://www.bkjia.com/PHPjc/631462.html www.bkjia.com true http://www.bkjia.com/PHPjc/631462.html techarticle object-oriented programming in PHP: How to develop a large PHP project (v) Author: Luis Argerich Translator: Limodou using classes for data storage a very good thing about PHP and OOP is that you can ...

  • 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.