Author Topic: OO Programming in PHP - Overloading (4 of 7)  (Read 214 times)

0 Members and 1 Guest are viewing this topic.

Offline JusticeLeague

  • Sr. Member
  • *
  • Posts: 352
  • Karma: +1/-0
    • View Profile
OO Programming in PHP - Overloading (4 of 7)
« on: August 31, 2008, 11:10:20 PM »
Object Oriented Programming in PHP - Overloading

(Page 4 of 7 )

Overloading (which is different from overriding) is not supported in PHP. In OOP you "overload" a method when you define two/more methods with the same name but different number or type of parameters (depending upon the language).

PHP is a loosely typed language so overloading by types won\'t work, however overloading by number of parameters doesn\'t work either.

It\'s very nice sometimes in OOP to overload constructors so you can build the object in different ways (passing different number of arguments). A trick to do something like that in PHP is:

Code: [Select]
<?php

class Myclass {
function 
Myclass() {
$name="Myclass".func_num_args();
$this->$name();
//Note that $this->$name() is usually wrong but here
//$name is a string with the name of the method to call.
}

function 
Myclass1($x) {
code;
}
function 
Myclass2($x,$y) {
code;
}
}

?>

With this extra working in the class the use of the class is transparent to the user:

Code: [Select]
$obj1=new Myclass(\'1\'); //Will call Myclass1
$obj2=new Myclass(\'1\',\'2\'); //Will call Myclass2

Sometimes this is very nice.

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.

Code: [Select]
<?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.

?>
« Last Edit: October 13, 2014, 02:45:06 AM by JusticeLeague »

Techronnati | where technology never sleeps

OO Programming in PHP - Overloading (4 of 7)
« on: August 31, 2008, 11:10:20 PM »

Mountain View

Offline MoneyRepublic

  • Full Member
  • *
  • Posts: 162
  • Karma: +0/-0
    • View Profile
Re: OO Programming in PHP - Overloading (4 of 7)
« Reply #1 on: October 13, 2014, 02:47:23 AM »
yung ganito bang polymorphism applicable lang sa php? sa javascript ba pwede eto?

I heard sa java applicable eto e.

 

Related Topics

  Subject / Started by Replies Last post
0 Replies
165 Views
Last post August 31, 2008, 11:06:03 PM
by JusticeLeague
0 Replies
176 Views
Last post August 31, 2008, 11:09:30 PM
by JusticeLeague
0 Replies
183 Views
Last post August 31, 2008, 11:11:47 PM
by JusticeLeague
0 Replies
148 Views
Last post August 31, 2008, 11:13:50 PM
by JusticeLeague
0 Replies
170 Views
Last post August 31, 2008, 11:20:10 PM
by JusticeLeague

Posting Disclaimer: Any individual may post a message in this forum and may do so anonymously. Therefore, the sole author is exclusively and entirely responsible for all opinions in that message. They do not represent the official opinions of Techronnati, its administrators or moderators or the Techronnati Management. Techronnati is merely acting as an impartial conduit for constitutionally protected free speech and is not responsible and will not be held liable for the content of such messages. All images and service logos are trademarks of their respective owners.