-3

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).

Osi
  • 1
  • 3
  • 9
  • 30

2 Answers2

0

You have declared everything (methods and properties) in the class private, but you can not access a private class property outside the class definition and that's why you got the error (Visibility in PHP).

To be able to access somthing of a class you should expose public method(s) inside the class. And that/those method can access/modify the private properties.

You should only declare a method or property private only when you want to keep its access private to the class so that you can control the modification of the properties or the class data.

Osi
  • 1
  • 3
  • 9
  • 30
Parantap Parashar
  • 1,930
  • 1
  • 14
  • 22
0

You defined getHeader and setHeader as private and you intend to use it from outside the class. This is bound to fail and you will need to make it public. Also, you intend to write the values of some private properties. You should not do so, use setHeader instead:

class Recipe {
    private $title = 'Yet another good recipe'; # Default recipe title.
    private $author = 'John Kelly'; # Default author.

    public function setHeader($title, $author) {
        $this->title = ucwords($title);
        $this->author = 'given to us by' . ucwords($author);
    }

    public function getHeader() {
        echo $this->title;
        echo $this->author;
    }
}

$recipe1 = new Recipe();
    $recipe1->setHeader('Korean Kimchi Pickles', 'Kim Jong Quei');
    $recipe1->getHeader();

$recipe2 = new Recipe();
    $recipe2->setHeader('Tunisian Salad Sandwich', 'Habib Ismail');
    $recipe2->getHeader();

Note, that it is a good approach to make your members private, but then you will need to use them through public methods. Also, I am not sure it is a good idea to echo the values. It might make more sense to return the values and echo them outside the class functions.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175