1

タイトルが理にかなっていることを願っています.PHPクラスの使用を学んでおり、かなりよく理解しています.読んだ/見たチュートリアルを超えて、配列を使用して値を返すようにしています.

私は純粋に実験しているだけで、なぜ私がやっていることは間違っているのか知りたいと思ってgetTheGameType()います.

//Just a manual array containing key/value pairs of games with their genres 
$array = array(
        "Grand Theft Auto" => "Action",
        "NBA 2k14" => "Sports",
        "COD" => "Shooting",
);

//My object
class videoGames {


    public $title;
    public $genere;

    public function setTheGameType($title,$genere) {

        $this->title = $title;
        $this->genere = $genere;
    }

    public function getTheGameType() {

            return 'The game genre for '.$this->title.' is:' . $this->genere;

    }

}

//New instance of `videoGames` class
$list = new videoGames();

//Here I set the game title with its genere
foreach ($array as $title => $genere) {
    $list-> setTheGameType($title,$genere);
}

//Echo the value passed into getTheGameType() function
    echo $list->getTheGameType();

上記は COD のゲーム ジャンルを返します: 配列の最後の値を取得するシューティング..

getTheGameType()基本的にメソッドをループしているすべてのキーと値のペアを返すにはどうすればよいですか?

編集: foreachループに追加することで機能し ました。echo $list->getTheGameType();

メソッドへの質問?これは悪い習慣ですか?

   foreach ($array as $title => $genere) {
    $list-> setTheGameType($title,$genere);
    echo $list->getTheGameType();
}
4

4 に答える 4

2

さて、あなたのvideoGamesクラスは本当に適切な名前が付けられていません。現在、1 つのビデオ ゲームのみを保存できます。ループを実行しているときは、オブジェクトのタイトルとジャンルのクラス プロパティをリセットしているだけです。

本当に OOP で作業したい場合は、おそらく 2 つのクラスが必要です。1 つはビデオ ゲーム用で、もう 1 つはビデオ ゲームのコレクションを表すためです。したがって、現在のクラスをそのままにして、名前を変更すると仮定しましょうvideo_game(単数形)。

次に、次のようにビデオ ゲームを格納するクラスを追加することができます。

class video_game_collection {
    protected $collection = array();

    // allow construction of collection by passing array of video_games (though not required)
    public __construct($game_array = null) {
        // validate all array elements are proper objects if the array is set
        if(is_array($game_array)) {
            foreach ($array as $game) {
                if ($game instanceof video_game === false) {
                    throw new Exception('You sent a date array element.');
                } else {
                    $this->collection[] = $game;
                }
            }
        }
    }

    public add_video_game($game) {
        if ($game instanceof video_game === false) {
            throw new Exception ('This is not a game.');
        }
        $this->collection[] = $game;
    }

    public get_collection() {
        return $this->collection;
    }
}

おそらく、クラス内でメッセージングをエコーするようなロジックを持つべきではありません (クラスを 1 つの目的 (データ オブジェクトを表すため) に限定します)。そのため、実際にテキストをエコーするのではなく、getGameType メソッドでゲーム タイプを返すことをお勧めします。メッセージはクラス外に任せましょう。

まとめると、次のようなことができます

$collection = new video_game_collection();

//Just a manual array containing key/value pairs of games with their genres 
$array = array(
        "Grand Theft Auto" => "Action",
        "NBA 2k14" => "Sports",
        "COD" => "Shooting",
);

foreach($array as $game => $genre) {
    $game = new videoGame();
    $game->setTheGameType($game, $genre);
    $collection->add_video_game($game);
}

これで、すべてのタイトルを返す、または追加機能を構築する場合、タイトルで並べ替える、特定のジャンルの映画を返す、すべてのジャンルを返すなどの操作を実行できるコレクションができました。

于 2013-09-13T23:54:46.803 に答える