#native_company# #native_desc#
#native_cta#

Object Oriented Programming in PHP: The Way to Large PHP Projects Page 6

By Luis Argerich
on January 25, 2008

Polymorphism

Polymorphism is defined as the ability of an object to determine which method to invoke
for an object passed as argument in runtime time. For example if you have a class figure
which defines a method draw and derived classes circle and rectangle where you override
the method draw you might have a function which expects an argument x and then call
$x->draw(). If you have polymorphism the method draw called depends of the type of
object you pass to the function.
Polymorphism is very easy and natural in interpreted languages as PHP (try to imagine
a C++ compiler generating code for this case, which method do you call? You don’t know
yet which type of object you have!, ok this is not the point). So PHP happily supports
polymorphism.

<?php

function niceDrawing($x) {

    
//Supose this is a method of the class Board.

    
$x->draw();

}

$obj=new Circle(3,187);

$obj2=new Rectangle(4,5);

$board->niceDrawing($obj);    //will call the draw method of Circle.

$board->niceDrawing($obj2);   //will call the draw method of Rectangle.

?>



OOP Programming in PHP

Some “purists” will say PHP is not truly an object oriented language, which is true.
PHP is a hybrid language where you can use OOP and traditional procedural programming.
For large projects, however, you might want/need(?) to use “pure” OOP in PHP declaring
classes, and using only objects and classes for your project.
As larger and larger projects emerge the use of OOP may help, OOP code is easy to
mantain, easy to understand and easy to reuse. Those are the foundations of
software engineering. Applying those concepts to web based projects is the key to
success in future web sites.

1
|
2
|
3
|
4
|
5
|
6
|
7
|
8
|
9