Object-oriented programming in PHP: Methods for developing large-scale PHP projects (3) Author: Luis Argerich Translator: limodou Overloading (different from overwriting) is not supported in PHP. In OOP, you can overload a method to implement two or more methods with the same name but different numbers or types of parameters (depending on the language). PHP is a loosely typed language, so overloading by type will not work, but overloading by different number of parameters will not work either. Sometimes it's nice to overload constructors in OOP so that you can create objects in different ways (passing different numbers of arguments). The trick to do it in PHP is: ----------------------------------------- --------------------------------------- $name(); //Note $this->name() is normally an error, but here $name is the name of the method that will be called} function Myclass1($x) { code; } function Myclass2($x,$y) { code; } } ?>------------------------------------------------ -------------------------------- Using this class is transparent to the user through additional processing in the class: $obj1=new Myclass(1); //Will call Myclass1 $obj2=new Myclass(1,2); //Will call Myclass2 Sometimes this is very useful. Polymorphism Polymorphism is an ability of an object, which can determine which object method to call based on the passed object parameters at run time. For example, if you have a figure class, it defines a draw method. And derived the circle and rectangle classes, in the derived class you override the draw method, you may also have a function, which expects a parameter x, and can call $x->draw(). If you have polymorphism, which draw method is called depends on the type of object you pass to the function. Polymorphism is very important in interpreted languages like PHP (imagine a C++ compiler generating code like this, which method should you call? You also don't know what type of object you have, well, that's not the point) Easy and natural. So of course PHP supports polymorphism. -------------------------------------------------- ------------------------------- draw(); } $obj=new Circle(3,187); $obj2= new Rectangle(4,5); $board->niceDrawing($obj); //The draw method of Circle will be called $board->niceDrawing($obj2); //The draw method of Rectangle will be called?>---- -------------------------------------------------- -------------------------- Object-oriented programming with PHP Some "purists" may say that PHP is not a true object-oriented programming The language of objects, that's a fact. PHP is a hybrid language. You can use OOP or traditional procedural programming. However, for larger projects, you may want/need to use pure OOP to declare classes in PHP, and only use objects and classes in your project. As projects get larger, it may be helpful to use OOP. OOP code is easy to maintain, easy to understand and reuse. These are the foundations of software engineering. Applying these concepts in web-based projects becomes the key to future website success. Reprinted from PHPBuilder.com