CodeBlanks Logo Icon CodeBlanks

Soru sınıfı tasarlama

PHP kursu · Orta seviye · Ders 43/115

Soru, seçenekler ve doğru cevabı tutan bir sınıf tasarlarsın.

Yapıcı metot ile soruyu kurmak

Bir soru yarattığında metin, seçenekler ve doğru cevap bilgisini birlikte vermek istersin. __construct metodu, new ile örnek oluşturulduğunda otomatik çalışır ve bu verileri property'lere atar.

Question sınıfında yapıcı metot üç parametre alır: string $text, array $options, int $answerIndex. İçinde $this->text = $text; gibi atamalarla parametreleri property'lere kopyalarsın. Böylece new Question("PHP nedir?", [...], 0) dediğinde soru hazır olur.

$this ile property başlatma

Yapıcı metot içinde $this sözde değişkeni, oluşturulmakta olan örneğe işaret eder. Parametreyi property'ye atamak için $this->text = $text; yazarsın. Aynı deseni options ve answerIndex için de tekrarlarsın.

Property'leri sınıfta önce tanımlamak iyi bir alışkanlıktır: public array $options = []; ve public int $answerIndex = 0;. Böylece tür bildirimi ve varsayılan değer açık olur, örnek her zaman tutarlı başlar.

Uygulama Yapıcı metot ekle

Question sınıfına public array $options = []; ve public int $answerIndex = 0; property'lerini ekle. Sonra public function __construct(string $text, array $options, int $answerIndex) yapıcı metodunu yaz; içinde $this->text = $text;, $this->options = $options;, $this->answerIndex = $answerIndex; atamalarını yap. Örneği $q = new Question("PHP nedir?", ["Betik dili", "Veritabanı", "Tarayıcı"], 0); olarak güncelle ve son satırı echo count($q->options); olarak değiştir.

Örnek çözümü göster
Sınıfa public array $options = []; ve public int $answerIndex = 0; ekle. public function __construct(string $text, array $options, int $answerIndex) { $this->text = $text; $this->options = $options; $this->answerIndex = $answerIndex; } yaz. $q = new Question("PHP nedir?", ["Betik dili", "Veritabanı", "Tarayıcı"], 0); ile örnek oluştur, echo count($q->options); ile yazdır.
Örnek çözüm:
<?php
// Question sınıfının iskeleti
class Question {
    public string $text = "";
    public array $options = [];
    public int $answerIndex = 0;

    public function __construct(string $text, array $options, int $answerIndex) {
        $this->text = $text;
        $this->options = $options;
        $this->answerIndex = $answerIndex;
    }
}

$q = new Question("PHP nedir?", ["Betik dili", "Veritabanı", "Tarayıcı"], 0);
echo count($q->options);

Kendini test et

  1. Question yapıcı metodu hangi verileri alır?

    • Yalnızca soru metnini
    • Soru metni, seçenekler ve doğru cevap indeksini
    • Yalnızca seçeneklerin sayısını
    • Sınıf adını ve dosya yolunu
    Cevabı göster

    Soru metni, seçenekler ve doğru cevap indeksini — Doğru. Yapıcı metot soru metnini, seçenek dizisini ve doğru cevap indeksini alır; hepsini property'lere atar.

  2. Yapıcı metoda parametre listesi ekle

    public function __construct(string ____) {
        $this->text = $text;
        $this->options = $options;
        $this->answerIndex = $answerIndex;
    }
    Cevabı göster
    public function __construct(string $text, array $options, int $answerIndex) {
        $this->text = $text;
        $this->options = $options;
        $this->answerIndex = $answerIndex;
    }
  3. Yapıcı metot kavramlarını eşleştir

    __construct
    Yapıcı metot adı
    $this->text
    Property ataması
    array $options
    Seçenekler parametresi
    new Question(...)
    Örnek oluşturma

Orta seviye bölümündeki tüm dersler