2013-03-12 7 views
8
function convert($currencyType) 
{ 
    $that = $this; 
    return $result = function() use ($that) 
    { 
     if (!in_array($currencyType, $this->ratio)) 
       return false; 

     return ($this->ratio[$currencyType] * $this->money); //a float number 
    }; 
} 

$currency = new Currency(); 
echo $currency->convert('EURO'); 

どういうところが間違っていますか?Closureクラスのオブジェクトをfilenameの文字列に変換できませんでした。

私は、エラーメッセージが出てい:

Catchable fatal error: Object of class Closure could not be converted to string 
+5

あなたの 'convert()'関数は関数を返します。それから、それを 'echo()'することによって文字列に変換しようとしています。 – landons

+0

しかし、クロージャは$ resultにfloat/falseを返しますか? –

+0

そして、Closure内の '$ this'参照は' $ that'でなければなりません。彼らは5.4の考えでそれを変えたかもしれない。わからない。 – landons

答えて

1

そこreturnを削除して実行します。

$result = function() use ($that) 
{ 
    if (!in_array($currencyType, $this->ratio)) 
      return false; 

    return ($this->ratio[$currencyType] * $this->money); //a float number 
}; 
return $result(); 

はまた、あなたが関数内で$thatを使用していません実現していますか?

ところで、なぜそこに無名関数が必要ですか?ただ、やる:あなたはラムダ関数を定義している

function convert($currencyType) 
{ 
    if (!in_array($currencyType, $this->ratio)) 
     return false; 

    return ($this->ratio[$currencyType] * $this->money); //a float number 
} 
+0

私は閉鎖でそれをやろうとしています。私は自分で閉鎖せずにそれを行うことができるので、それは必要ではないことを知っています。私はちょうど学習目的のためにクロージャを使用したいと思う。 –

+0

@LisaMiskovsky、クロージャは変数に格納できます。したがって '$ var = function(){...}'はその関数を変数に格納します。これは、匿名関数を使用する場所ではありません。最初から悪いことを学ばないでください。閉鎖が必要になったら、それらを使用する方法がわかります。 – Shoe

+0

クロージャを使用するのに適した例を教えていただけますか? –

0
class Currency { 
    function convert($currencyType) 
    { 
     if (!in_array($currencyType, $this->ratio)) 
      return false; 
     return ($this->ratio[$currencyType] * $this->money); //a float number 
    } 
} 

$currency = new Currency(); 
echo $currency->convert('EURO'); 

。あなたはそれを必要としません。また、正確性を保つためにbcmul()を使用する必要があります。 PHPの浮動小数点演算はあなたにファンキーな結果を与えるでしょう。

5

問題のカップル:その後、

  1. あなたはクロージャを返しているので、あなたが最初の変数に閉鎖を割り当てる必要があり、かつ
  2. あなた$this参照がAの内部で動作しません関数を呼び出します
  3. あなたはまた、クロージャのスコープでそれにアクセスするために$currencyTypeを使用する必要があります(あなたはuseではなく$thatをINGのだ理由です)閉鎖

function convert($currencyType) 
{ 
    $that =& $this; // Assign by reference here! 
    return $result = function() use ($that, $currencyType) // Don't forget to actually use $that 
    { 
     if (!in_array($currencyType, $that->ratio)) 
       return false; 

     return ($that->ratio[$currencyType] * $that->money); //a float number 
    }; 
} 

$currency = new Currency(); 
$convert = $currency->convert('EURO'); 
echo $convert(); // You're not actually calling the closure until here! 
+0

一つ質問しかし、。なぜ(!in_array($ currencyType、$ that-> ratio))がfalseを返すのか?常にfalseを返しますか? $ currencyType = 'EURO'とprint_r($ that-> ratio)は配列を正しく出力します。 –

2

機能を閉じるときは、かっこで機能させ、かっこを追加する必要があります。

function convert($currencyType) 
{ 
    $that = $this; 
    return $result = (function() use ($that) 
    { 
     if (!in_array($currencyType, $this->ratio)) 
       return false; 

     return ($this->ratio[$currencyType] * $this->money); //a float number 
    })(); 
} 

$currency = new Currency(); 
echo $currency->convert('EURO'); 
関連する問題