I'm trying to get my head wrapped around all the concepts of OOP. I have heard that it is better to use private properties and use getting methods rather then using public properties. It seems like it has the same result but I am trying to understand why one is any better then the other. In the two class samples below I have done it both ways and wanted to see if someone could tell me why the second one would be better then the first one.
If it is easier to show the reason with another sample that is fine, I just thought it might make more sense to me this way.PHP Code:
class Movie
{
public $movie_id
public $movie_name;
public $rating;
public $genre;
public $director;
public function getMovie($id)
{
$sql = "select * from movie where movie_id = $this->movie_id";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$this->movie_name = $row['movie_name'];
$this->rating = $row['rating'];
$this->genre = $row['genre'];
$this->director = $row['director'];
}
}
class Movie
{
private $movie_id
private $movie_name;
private $rating;
private $genre;
private $director;
public function getMovie()
{
$sql = "select * from movie where movie_id = $this->movie_id";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$this->movie_name = $row['movie_name'];
$this->rating = $row['rating'];
$this->genre = $row['genre'];
$this->director = $row['director'];
}
public function getMovieName()
{
return $this->movie_name;
}
public function getRating()
{
return $this->rating;
}
public function getGenre()
{
return $this->genre;
}
public function getDirector()
{
return $this->director;
}
}
Thanks


Reply With Quote







Bookmarks