Implementing object-Oriented Programming in PHP (_php) Tutorial

Source: Internet
Author: User
Polymorphism
Polymorphism is defined as when an object is passed as a parameter at run time, the object can determine the ability to invoke that method. For example, using a class to define the method "draw", the inheriting class redefined the "draw" behavior to draw a circle or a square, so you have a function with the parameter x, which can be called $x->draw () in the function. If polymorphism is supported, then the call to the "draw" method depends on the type of Object X. Polymorphism is naturally supported in PHP (think about this when compiling in the C + + compiler, which method is called?). However, you do not know what the type of object is, of course, this is not the case now.
Fortunately, PHP supports polymorphism.
function nicedrawing ($x) {
Supose This is a method of 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 for PHP
The Pure object theory thinks that PHP is not a true object-oriented language, which is right. PHP is a mixed language, and you can use it in object-oriented or traditional architecture programming methods. For large projects, however, you might or would need to define classes using a purely object-oriented approach, and use only objects and classes in your project. Increasingly large projects benefit from using an object-oriented approach, and object-oriented engineering is very easy to maintain, easy to understand and reuse. This is the basic of software engineering. Using these concepts is the key to future success in web design.
Advanced object-oriented technology in PHP
After reviewing the basic object-oriented concepts, I'll cover some of the more advanced techniques.
Serialization
While PHP does not support persistent objects, persistent objects in object-oriented languages are objects that have been repeatedly called by the application to maintain their state and functionality, which means that there is a way to save the object to a file or database and then reload the object. This mechanism is called serialization. PHP has a serialization function that can be called in the object, and the serialization function returns a String representing the object. The serialization function then saves the member data instead of the member function.
In PHP4, if you serialize an object to a string $s and then delete the object and then crossdress the object to $obj, you can still call the object's method function. But I do not recommend this method, 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 crossdress this object and want the object's method function to still be valid because the serialized string does not represent any member function. Finally, it is very useful to serialize the member variables of the saved object in PHP, just so. (You can serialize union arrays and arrays into disk).

http://www.bkjia.com/PHPjc/631809.html www.bkjia.com true http://www.bkjia.com/PHPjc/631809.html techarticle Polymorphism polymorphism is defined as when an object is passed as a parameter at run time, the object can determine the ability to invoke that method. For example, the method "draw" is defined with a class, inheriting the class ...

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