I've been reading up on OO PHP programming and encapsulation and I'm still finding it a little confusing.
I have this code:
class Item {
private $id;
private $description;
public function __construct($id) {
$this->id = $id;
}
public function getDescription() {
return $this->$description;
}
public function setDescription($description) {
$this->description = $description;
}
}
In my testclass.php file, when I use the set and get Description functions like so:
$item = new Item(1234);
$item->setDescription("Test description");
echo $item->getDescription();
I get an error saying Undefined variable: description. Could someone please explain to me why this is, because I thought the point of a set method was to define the variable? I thought you declare the variable in the class, and then you define the variable when you use the set method so that it can be accessed with the get method?