2012-02-17 14 views
1

Netbeansが最後の行の3番目に中括弧でエラーメッセージを投げているのはちょっと疑問に思っていますか?助けてくれてありがとう。Netbeansが構文エラーを投げる - PHP

<?php 

    class convert{ 

var $amnt = htmlspecialchars($_GET["amnt"]); 
var $cc_from = htmlspecialchars($_GET["from"]); 
var $cc_to = htmlspecialchars($_GET["to"]); 

function convert($amnt,$cc_from,$cc_to,$decimals=2){ 
$db_rate_from = mysql_query("SELECT * FROM $db_tableprefix WHERE country_code='$cc_from'"); 
$query_row_from = mysql_fetch_array($db_rate_from); 
$rate_from = ($query_row_from[rate]); 
echo $rate_from; 
echo "</br>rate to</br>"; 

$db_rate_to = mysql_query("SELECT * FROM $db_tableprefix WHERE country_code='$cc_to'"); 
$query_row_to = mysql_fetch_array($db_rate_to); 
$rate_to = ($query_row_to[rate]); 
echo $rate_to; 
echo "</br>conversion</>"; 

var $conversion = (number_format(($amnt/$rate_from)*$rate_to,$decimals)); 
echo $conversion; 

} 
} 
?> 
+1

選択した回答では、コードに関するすべての問題は解決されません。関数内の 'var'もエラーを引き起こします(エラーフリーコードの私の答えを見てください)。 – 0b10011

答えて

1

クラス内の変数を宣言することはできません。次のようなものが欲しい:

class convert 
{ 

    public $amnt; // don't worry about what public means 
    public $cc_from; // if you want to know, have a look at 
    public $cc_to; // http://php.net/public 

    // This runs when the class is created with `new convert()` 
    public function __construct() 
    { 
     $this->amnt = htmlspecialchars($_GET["amnt"]); // sets that $amnt up there 
     $this->cc_from = htmlspecialchars($_GET["from"]); 
     $this->cc_to = htmlspecialchars($_GET["to"]); 
    } 

    // All that's changed here is making $amnt into $this->amnt, etc 
    function convert($amnt,$cc_from,$cc_to,$decimals=2) 
    { 
     $db_rate_from = mysql_query("SELECT * FROM $db_tableprefix WHERE country_code='$this->cc_from'"); 
     $query_row_from = mysql_fetch_array($db_rate_from); 
     $rate_from = ($query_row_from[rate]); 
     echo $rate_from; 
     echo "</br>rate to</br>"; 

     $db_rate_to = mysql_query("SELECT * FROM $db_tableprefix WHERE country_code='$cc_tothis->'"); 
     $query_row_to = mysql_fetch_array($db_rate_to); 
     $rate_to = ($query_row_to[rate]); 
     echo $rate_to; 
     echo "</br>conversion</>"; 

     $conversion = (number_format(($this->amnt/$rate_from) * $rate_to, $decimals)); 
     echo $conversion; 
    } 

} 
+0

これは間違っています。関数内の 'var'も同様にエラーをスローします。 – 0b10011

+0

それを逃した、良いキャッチ。一定。 – Joe

0

コードにはいくつかの誤りがあります。これを改訂してみてください。

<?php 

    class convert { 

     var $amnt; 
     var $cc_from; 
     var $cc_to; 

     function convert($amnt,$cc_from,$cc_to,$decimals=2){ 

      $this->amnt = $amnt; 
      $this->cc_from = $cc_from; 
      $this->cc_to = $cc_to; 

      $db_rate_from = mysql_query("SELECT * FROM $db_tableprefix WHERE country_code='$cc_from'"); 
      $query_row_from = mysql_fetch_array($db_rate_from); 
      $rate_from = ($query_row_from[rate]); 
      echo $rate_from; 
      echo "</br>rate to</br>"; 

      $db_rate_to = mysql_query("SELECT * FROM $db_tableprefix WHERE country_code='$cc_to'"); 
      $query_row_to = mysql_fetch_array($db_rate_to); 
      $rate_to = ($query_row_to[rate]); 
      echo $rate_to; 
      echo "</br>conversion</>"; 

      $conversion = (number_format(($amnt/$rate_from)*$rate_to,$decimals)); 
      echo $conversion; 
     } 
    } 

    $example = new convert(htmlspecialchars($_GET["amnt"]), htmlspecialchars($_GET["from"]), htmlspecialchars($_GET["to"])); 

推論: あなたが直接クラスに値を取得引っ張ることはできません。変数をインスタンス化されたクラスに渡す必要があります。

$ example変数はインスタンス化されたクラスを保持します。

1

クラス変数は定数である必要があります。それらは__construct()で別のものに設定することができます。また、関数内でvarを使用することはできません。私は以下のすべての変更についてコメントしました。

<?php 
class convert { 
    var $amnt = ""; // Set to "" 
    var $cc_from = ""; // Set to "" 
    var $cc_to = ""; // Set to "" 
    // Added __construct() to set defaults when initialized 
    function __construct(){ 
     $this->amnt = htmlspecialchars($_GET["amnt"]); 
     $this->cc_from = htmlspecialchars($_GET["from"]); 
     $this->cc_to = htmlspecialchars($_GET["to"]); 
    } 
    function convert($amnt, $cc_from, $cc_to, $decimals=2){ 
     $db_rate_from = mysql_query("SELECT * FROM $db_tableprefix WHERE country_code='$cc_from'"); 
     $query_row_from = mysql_fetch_array($db_rate_from); 
     $rate_from = ($query_row_from[rate]); 
     echo $rate_from; 
     echo "</br>rate to</br>"; 
     $db_rate_to = mysql_query("SELECT * FROM $db_tableprefix WHERE country_code='$cc_to'"); 
     $query_row_to = mysql_fetch_array($db_rate_to); 
     $rate_to = ($query_row_to[rate]); 
     echo $rate_to; 
     echo "</br>conversion</>"; 
     $conversion = (number_format(($amnt/$rate_from)*$rate_to,$decimals)); // Removed 'var' 
     echo $conversion; 
    } 
} 
?> 
関連する問題