$this variable and functions example

<?php
class person
{
public $heir_color;
public $height;
public $person_name;

public function set($hc,$h,$pn)
{
$this->heir_color = $hc;
$this->height = $h;
$this->person_name = $pn;
}
public function display()
{
echo “Person Name: “.$this->person_name.”<br>”;
echo “Person Height: “.$this->height.”<br>”;
echo “Person Heir Color: “.$this->heir_color.”<br>”;
}
}

$tanvir = new person();
$tanvir->set(‘black’,5.6,’tanvir’);
$tanvir->display();
$elahi = new person();
$elahi->set(‘white’,5.6,’elahi’);
$elahi->display();

?>

 

In this example , you can see how object oriented  php help us to get rid of writing same code .. its maintain DRY (don’t repeat yourself) .. only changing value we get results.
$this variable or keyword used to mention properties within a function/method whatever you say .. one can’t use the property or variable name directly in a method within class its mandatory to use $this keyword/variable …

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