PHP object-oriented programming: method for developing large PHP projects (3) (reproduced)
Source: Internet
Author: User
PHP object-oriented programming: method for developing large PHP projects (3)
Author: Luis Argerich translator: limodou
Overload (different from overwriting) is not supported in PHP. In OOP, you can overload a method to implement two or more methods with the same
But there are different numbers or types of parameters (depending on the language ). PHP is a loose type language.
However, the number of parameters does not work.
Sometimes the overload constructor in OOP is very good, so that you can create objects using different methods (passing different numbers of parameters ). In PHP
To implement it:
--------------------------------------------------------------------------------
<? Php
Class Myclass {
Function Myclass (){
$ Name = "Myclass". func_num_args ();
$ This-> $ name ();
// Note that $ this-> name () is generally incorrect, but $ name is the name of the method to be called.
}
Function Myclass1 ($ x ){
Code;
}
Function Myclass2 ($ x, $ y ){
Code;
}
}
?> --------------------------------------------------------------------------------
Through additional processing in the class, using this class is transparent to users:
$ Obj1 = new Myclass ('1'); // call Myclass1
$ Obj2 = new Myclass ('1', '2'); // call Myclass2
Sometimes this is very useful.
Polymorphism
Polymorphism is a capability of an object. It determines the method of the object to be called at runtime based on the passed object parameters. For example,
If you have a figure class, it defines a draw method. The circle and rectangle classes are derived.
With the draw method built, you may have another function that wants to use a parameter x and can call $ x-> draw (). If you have polymorphism,
The draw method that you call depends on the object type that you pass to this function.
Polymorphism is in an interpreted language like PHP (imagine which method should you call when a C ++ compiler generates such code? You
It is very easy and natural to know what type of objects you own. Therefore, PHP certainly supports polymorphism.
--------------------------------------------------------------------------------
<? Php
Function niceDrawing ($ x ){
// Assume this is a method of the Board class.
$ X-> draw ();
}
$ Obj = new Circle (3,187 );
$ Obj2 = new Rectangle (4, 5 );
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