Use Private member to hide information in PHP
Description
The following code shows how to use Private member to hide information.
Example
/*w ww. ja va 2 s . co m*/
<?php
class Widget
{
private $name;
private $price;
private $id;
public function __construct($name, $price)
{
$this->name = $name;
$this->price = floatval($price);
$this->id = uniqid();
}
//checks if two widgets are the same
public function equals($widget)
{
return(($this->name == $widget->name) AND
($this->price == $widget->price));
}
}
$w1 = new Widget('Cog', 5.00);
$w2 = new Widget('Cog', 5.00);
$w3 = new Widget('Gear', 7.00);
//TRUE
if($w1->equals($w2))
{
print("w1 and w2 are the same<br>\n");
}
//FALSE
if($w1->equals($w3))
{
print("w1 and w3 are the same<br>\n");
}
//FALSE, == includes id in comparison
if($w1 == $w2)
{
print("w1 and w2 are the same<br>\n");
}
?>
The code above generates the following result.