-2

I am new to Php OOP's concept..i am trying to print a book name in a table(index.php) file which the book details are in different class file (FirstExample.php).I got parse error in class file.I Check my code twice..But i dont understand mistake i have done..

<?php
class FirstExample {
var $book;
var $price;
public  function getBookName($bookname){
     $this->book = $bookname;
     echo "The bookname you Ordered is $book";
}
public  function getBookPrice($bookPrice){
    $this->price = $bookPrice;
    echo "The Book Price is $price";
}
$physics = new FirstExample;
$chemistry = new FirstExample;
$Mathematics = new FirstExample;
$computerProgramming = new FirstExample;
$physics->getBookName("Physics");
$physics->getBookPrice("$150");
$chemistry->getBookName("Chemistry");
$chemistry->getBookPrice("$100");
$Mathematics->getBookName("Mathematics-Differential and Integral Calculas");
$Mathematics->getBookPrice("$200");
 $computerProgramming->getBookName("Advanced Computer Programming");
$computerProgramming->getBookPrice("$200");
}
?>
Dave
  • 3,073
  • 7
  • 20
  • 33
Madhi
  • 45
  • 1
  • 12

1 Answers1

1
<?php
//class starts here 
class FirstExample {

private $book;// use private|protected|public (public is default) keyword not var in class 
private $price;

    public  function getBookName($bookname){
         $this->book = $bookname;
         echo "The bookname you Ordered is $this->book";// if variable is private the use $this->variable_name to access it in side of class 
         // out side of class protected variable is not accessable.
    }

    public  function getBookPrice($bookPrice){
        $this->price = $bookPrice;
        echo "The Book Price is $this->price";
    }
}//class ends here 
$physics = new FirstExample;
$chemistry = new FirstExample;
$Mathematics = new FirstExample;
$computerProgramming = new FirstExample;
$physics->getBookName("Physics");
$physics->getBookPrice("$150");
$chemistry->getBookName("Chemistry");
$chemistry->getBookPrice("$100");
$Mathematics->getBookName("Mathematics-Differential and Integral Calculas");
$Mathematics->getBookPrice("$200");
$computerProgramming->getBookName("Advanced Computer Programming");
$computerProgramming->getBookPrice("$200");

?>
viral barot
  • 631
  • 4
  • 8