Implementing object-Oriented programming in PHP (next)

Source: Internet
Author: User
Tags define object copy explode functions variables php and string
Programming | Object polymorphism

Polymorphism is defined as the ability of an object to invoke that method when an object is passed as a parameter at run time. For example, a class that defines the method "draw", inherits the class to redefine the behavior of the "draw" to draw a circle or a square, so that you have a function with a parameter of x, which can call $x->draw () in a function. If polymorphism is supported, the invocation of the "draw" method depends on the type of Object X. Polymorphism is naturally supported in PHP (think about this situation. If compiled in the C + + compiler, which method is invoked?) However, you do not know what type of object is, of course, this is not the case now.

Fortunately, PHP supports polymorphism.

function nicedrawing ($x) {
Supose This is the class Board.
$x->draw ();
}

$obj =new Circle (3,187);
$obj 2=new Rectangle (4,5);

$board->nicedrawing ($obj); Would call the draw method of Circle.
$board->nicedrawing ($obj 2); Would call the draw method of Rectangle.

? >
Object oriented Programming in PHP

Pure object-oriented people think that PHP is not a real object-oriented language, this is true. PHP is a hybrid language that you can use with object-oriented or traditional structural programming. For large projects, however, you might or need to use a purely object-oriented approach to define classes and use only objects and classes in your project. Increasingly large projects benefit from the use of object-oriented methods, which are easy to maintain, easy to understand and reuse. This is the basic of software engineering. Using these concepts in web design is the key to future success.

Advanced object-oriented Technology in PHP

After reviewing the basic object-oriented concepts, I'll introduce some of the more advanced techniques.

Serialization

PHP does not support persistent objects, and persistent objects in object-oriented languages are objects that have been invoked many times by applications that still maintain their state and functionality, meaning that there is a way to save objects to a file or database and then reload the object. This mechanism is called serialization. PHP has a serialized function that can be invoked in an object, and the serialization function returns a String representing the object. The serialization function then holds the member data rather than the member function.

In PHP4, you can still call the object's method function if you serialize an object to a string $s, then delete the object, and then drag the row object to the $obj. But I do not recommend this approach because (a) This feature does not necessarily support in the future (b) This leads to an illusion if you save the serialized object to disk and exit the program. When you rerun this script in the future, you cannot drag this object and expect the object's method function to remain valid because the serialized string does not represent any member functions. Finally, serializing the member variables of the saved objects is useful in PHP, just so. (You can serialize the union array and arrays to disk).

Example:

$obj =new Classfoo ();
$str =serialize ($obj);
Save $str to disk

... some months later

Load str from disk
$obj 2=unserialize ($STR)

? >
In the example above, you can recover a member variable without a member function (according to the document). This causes $obj 2->x to be the only way to access member variables (because there are no member functions).

There are a few ways to solve this problem, but I'll leave it to you because it will mess up this clean document.

I want PHP to fully support serialization in the future.

Using classes to manipulate saved data

A good place in PHP and object-oriented programming is that you can easily define a class to manipulate something and invoke the appropriate class when needed. If you have an HTML file, you need to select a product by selecting its ID number, and your data is stored in the database, and you want to display information about the product, such as price and so on. You have different kinds of products and the same actions have different meanings for different products.

For example, displaying a sound means playing it, while for other products it is likely to display a picture stored in the database. You can use object-oriented programming and PHP to achieve less code but better.

Define a class, define the methods that the class should have, and then define the class (Sounditem class, Viewableitem class, etc.) of each product by inheritance, and redefine the methods of each product class so that they are as you want. Define a class for each product type based on the Product Type field of the table you saved in the database, and a typical product table should have fields (ID, type, price, description, etc.).

In the script you get the type information from the table in the database, and then instantiate the object of the corresponding class:

$obj =new $type ();
$obj->action ();

? >
This is a comparison of PHP features, you can call the $obj display method or other methods without going to the type of object. With this technique, when you add a new type of object, you don't need to modify the script. This method is a bit powerful, is to define the methods that all objects should have, regardless of their type, and then implement them in different ways in different classes, so that you can use them in scripts for different types of objects, no if, no two programmers in the same file, always happy. Do you believe programming is so happy? Low maintenance cost and reusable?

If you lead a group of programmers, the best approach is to divide the tasks, and each person can be responsible for a particular category and object. Internationalization can be solved with the same technology, so that the appropriate classes correspond to the different languages chosen by the user, and so on.

Replication and cloning

When you create an object $obj, you can use $obj 2 = $obj to copy an object, the new object is a copy of the $obj (not a reference), so the new object after the assignment has $obj the same new state. Sometimes you don't want to, just want to create the same new object as obj, call the constructor of the new object as if you had used the command. This can be achieved through the serialization of PHP and the use of base classes and other classes that must inherit from the base class.

In a dangerous zone.

When you serialize an object, you get a string that has a specific format, and if you are curious, you may find the secret, one of the things in the string is the name of the class, and you can undo it:

$herring =serialize ($obj);
$vec =explode (:, $herring);
$nam =str_replace ("\" ", \, $vec [2]);

? >
Suppose you create a class "universe" and make all classes inherit from "Universe", you can define a clone in "universe":

Class Universe {
function Clone () {
$herring =serialize ($this);
$vec =explode (:, $herring);
$nam =str_replace ("\" ", \, $vec [2]);
$ret =new $nam;
return $ret;
}
}

Then:

$obj =new something ();
Something Extends Universe!!
$other = $obj->clone ();

? >
What you get is the new object of class something like using new, and constructors are called and so on. I don't know if it's useful to you, it's a good practice, Universe class knows the name of its inheriting class. For you, the only limit is your imagination!!!

Note: I am using PHP4, and there are some things in the article that may not be suitable for PHP3.

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.