2016-12-05 85 views
-1

我是Php OOP的概念新手..我試圖在一個表(index.php)文件中打印書名,該表中的書詳細信息位於不同的類文件中(FirstExample.php)。我得到了在類解析錯誤file.I檢查我的代碼twice..But我不明白的錯誤,我已經做了..解析錯誤:意外(T_variable)期望函數(T_FUNCTION)

<?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"); 
} 
?> 
+1

關閉與'類}'getBookPrice後' '定義 –

+1

移動}根據函數getBookPrice(....)...關閉你的類定義 – donald123

+0

謝謝@ donald123和@ Nordenheim – Madhi

回答

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"); 

?> 
+0

非常感謝.. :) @viral barot – Madhi

相關問題