function overloading

<?php

/*Those who is from CSE background may know in C++ we got some knowledge about function overloading … that is two or more function of the same name in the same class at a time … they only differ by their parameter number or type ..
In object oriented php we do not have to such thing .. cause we can use facilities of function overloading using some other way .. Below example give you a sample that utilize the same thing like function overlaoding ..*/
class hacker
{
public $hacker_name;
public $hacker_age;

public function set($hn)
{
if(is_string($hn))
$this->hacker_name = $hn;
else
$this->hacker_age = $hn;
}
public function get()
{
if(isset($this->hacker_name))
echo “Name: “.$this->hacker_name;
else
echo “Age: “.$this->hacker_age;
}

}

$hacker1 = new hacker();
$hacker1->set(23);
$hacker1->get();
echo “<br/>”;
$hacker2 = new hacker();
$hacker2->set(‘Tanvir’);
$hacker2->get();
?>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s