Lets say i have a class like this
class book {
public $title;
public $author;
public $page_num;
.....
....
public function show_book($limit=1){
....
$result = mysql_query("SELECT title,author,page_num FROM book limit=$limit");
while($row = mysql_fetch_array($result))
{
$this->title=$row['title'];
$this->author=$row['author'];
$this->page_num=$row['page_num'];
}
}
and then i can use it like :
$book = new book();
$book->show_book();
echo $book->title." ".$book->author ...... ;
this works for one book
what if i want to show 7 books ?
this is not working for $book->show_book($limit=7);
i can make 7 objects but i think there’s a better way
I’d rename the function
show_booksand have it return an array ofbookobjects. When you have no matches you return an empty array, otherwise you return all of the matching elements. It probably makes the most sense as a static method rather than an instance method.