I am getting into OOP and I run into the following dilemma. I have the a class:
class Recipe {
var $title;
var $time;
var $ingredients = array();
var $instructions;
var $category;
function __construct($title, $time, $ingredients, $instructions, $category) {
$this->title = $title;
...
}
function getTitle() {
return $this->title;
}
}
All the properties are public (by default). Do I have to define accessor methods for all these properties (e.g. getTitle) or can I just refer to the properties directly, like this:
...
$recipe = new Recipe($title, $time, $ingredients, $instructions, $category);
echo $recipe->title; // versus $recipe->getTitle();
It looks like I will save lots of time not having to define accessor methods. However I wonder what are the pros and cons of this kind of approach?