I have trouble learning PHP and trying to write a class
of a recipe pamplet. Each recipe in the pamplet has a header comprised of title and author.
In each instance, I try to give a different title and author property value but fail and I don't know where is my mistake.
class Recipe {
private $title = 'Yet another good recipe'; # Default recipe title.
private $author = 'John Kelly'; # Default author.
private function setHeader($title, $author) {
$this->title = ucwords($title);
$this->author = 'given to us by' . ucwords($author);
}
private function getHeader() {
echo $this->title;
echo $this->author;
}
}
$recipe1 = new Recipe();
$recipe1->title = 'Korean Kimchi Pickles';
$recipe1->author = 'Kim Jong Quei';
$recipe1->getHeader();
$recipe2 = new Recipe();
$recipe2->title = 'Tunisian Salad Sandwich';
$recipe2->author = 'Habib Ismail';
$recipe2->getHeader();
Uncaught Error: Cannot access private property ... on line 19.
I desire to ask why I get this error if I made sure all class aspects (properties/methods) are private to that class, or at least, thus it seems to me as a freshman...
Update:
I thought the data (methods) are private for instances of other classes, not of that class, but I was wrong, they're private even for instances of that particular class. This is why I missed the intention of the error and reading the answers, I understood I could access private properties through public methods (though I can make all data public as well).