2017-02-27 6 views
-2
<?php 
class Dribble { 
    public $shotCount = 0; 
    private $shot = false; 
    public function shotPosted() { 
     $shot = true; 
     if ($shot == true) { 
      $shotCount++; 
      echo $shotCount; 
     } 
     if ($shotCount >= 7) { 
     exit("You've reached your monthly goal!"); 
     } 
    } 
} 
$shot1 = new Dribble(); 
$shot1->shotPosted(); 
$shot2 = new Dribble(); 
$shot2->shotPosted(); 

私はオブジェクト指向のPHPには少し新しく、現在私はちょっと立ち往生している問題に取り組んでいます。 入力があれば幸いです。前もって感謝します。このためオブジェクト指向カウンタ

+0

'if($ shot = true)'は 'if($ shot == true)'にする必要があります。 '='は代入、 '=='は比較です。 – Barmar

+0

あなたの関数 – cmorrissey

答えて

-1

-

if ($shot = true) { ... } 

あなたはおそらく、この意味は:

if ($shot == true) { ... } 

=割り当てのために、==平等をテストすることです。 ===同じタイプのものもテストします。

+0

の中にある全てのインスタンスに対して '$ this-> shotCount'を使う必要がありますが、これは彼のコードでは問題ではないか答えは – cmorrissey

+0

彼は実際に彼の問題が何であるか言っていませんでした。左推測... –

0

オブジェクトのプロパティにアクセスするには、メソッドで->という表記法を使用する必要があります。だから$shotCount$this->shotCountになるはずです。

class Dribble { 
    public $shotCount = 0; 
    public function shotPosted() { 
     $this->shotCount++; 
     echo $this->$shotCount; 
     if ($this->$shotCount >= 7) { 
     echo "You've reached your monthly goal!"; 
     $this->shotCount = 0; 
     } 
    } 
} 

また、関数内でexit()を呼び出すべきではありません。指示はshotCount0に設定する必要がありますが、スクリプトを終了するとすべてが失われます。

関連する問題