2016-04-13 15 views
1

私はポーカーゲームを表すゲームクラスを作成しました。私はdbからそれを読み込み、属性NoOfPlayersの1つを表示しようとしていますが、動作させることができません。ここにクラスがあります。オブジェクトを作成してその属性にアクセスするPHPの問題

class pokerGame { 

public $GameID, $State, $BuyIn, $Ante, $NoOfPlayers; 

function __construct() { 
    $GameID = $BuyIn = $Players = $Ante = 0; 
    $State = ""; 
} 

function withID($newGameID) { 
    $this->read($newGameID);  
} 

function read($newGameID) 
{ 
    global $conn; 

    $query = "SELECT * FROM games WHERE GameID = ".$newGameID." LIMIT 1"; 
    $result = $conn->query($query) or die('Error getting values: '.$query); 

    $response = array(); 

    if ($result->num_rows == 0) { return;} 

    $row = $result->fetch_assoc(); 

    $GameID = $row["GameID"]; 
    $State = $row["State"]; 
    $BuyIn = $row["BuyIn"]; 
    $Ante = $row["Ante"]; 
    $NoOfPlayers = $row["NoOfPlayers"]; 
} 

function write($buyIn,$ante) 
{ 
    global $conn; 

    $query = "INSERT INTO games (BuyIn,Ante) VALUES ('$buyIn','$ante')";  

    $conn->query($query) or die('Error Inserting Values: '.$query); 

} 

function getNoOfPlayers() { 
    return $this->NoOfPlayers; 
} 

} 

ここにどのようにアクセスしようとしています。

$thisPokergame = new pokerGame(); 
    $thisPokergame = $thisPokergame->withID("3"); 

    echo $thisPokergame->getNoOfPlayers(); 

私のオブジェクトがNULLであるように私にエラーがクラスであるかなり確信して、私はget_object_varsメソッドを使用して属性を表示してみました。

+0

呼び出すという実際の問題とは何ですか?あなたはどんなエラーを出していますか? – Jon

+0

キャッチされていないエラー:ヌルのメンバー関数getNoOfPlayers()を呼び出します。 –

+0

基本的にオブジェクトがnullです –

答えて

1

あなたのnoOfPlayersが決して設定されないように、あなたのメンバー変数にアクセスしていないことが適切です。 変更

$NoOfPlayers = $row["NoOfPlayers"]; 

これにこれ。

$this->NoOfPlayers = $row["NoOfPlayers"]; 

てみませんか、あなたのメンバ変数のすべてについて

関連する問題