CodeBlanks Logo Icon CodeBlanks

Post sınıfı oluşturma

PHP kursu · Orta seviye · Ders 66/115

Başlık, içerik ve yazar tutan bir Post sınıfı tasarlarsın.

Post sınıfı property'leri

Bir yazı (post), başlık, içerik ve yazar olmak üzere üç temel veri taşır. Post sınıfında bu veriler için üç property tanımlarsın: public string $title = "";, public string $content = ""; ve public string $author = "";. Her property boş dize varsayılanı ile başlar.

Property'leri sınıf içinde açıkça tanımlamak iyi bir alışkanlıktır: tür string ve varsayılan değer "" net olur. Böylece örnek her zaman tutarlı başlar ve hata ayıklama kolaylaşır.

__construct ile kurulum

Üç property'yi tek tek elle atamak yerine, __construct metodu ile örnek oluştururken üç değeri birlikte verebilirsin. public function __construct(string $title, string $content, string $author) yazıp içine $this->title = $title; gibi atamalar koyarsın.

Böylece new Post("İlk yazım", "Merhaba dünya", "Ayşe") dediğinde yazı hazır olur. $this sözde değişkeni, oluşturulmakta olan örneğe işaret eder ve parametreyi property'ye kopyalamanı sağlar.

Uygulama Property'ler ve yapıcı metot ekle

Post sınıfına public string $content = ""; ve public string $author = ""; property'lerini ekle. Sonra public function __construct(string $title, string $content, string $author) yapıcı metodunu yaz; içinde $this->title = $title;, $this->content = $content;, $this->author = $author; atamalarını yap. Örneği $p = new Post("İlk yazım", "Merhaba dünya", "Ayşe"); olarak güncelle ve son satırı echo $p->title . " - " . $p->author; olarak değiştir.

Örnek çözümü göster
Sınıfa public string $content = ""; ve public string $author = ""; ekle. public function __construct(string $title, string $content, string $author) { $this->title = $title; $this->content = $content; $this->author = $author; } yaz. $p = new Post("İlk yazım", "Merhaba dünya", "Ayşe"); ile örnek oluştur, echo $p->title . " - " . $p->author; ile yazdır.
Örnek çözüm:
<?php
// Post sınıfına property'ler ve yapıcı metot ekle
class Post {
    public string $title = "";
    public string $content = "";
    public string $author = "";

    public function __construct(string $title, string $content, string $author) {
        $this->title = $title;
        $this->content = $content;
        $this->author = $author;
    }
}

$p = new Post("İlk yazım", "Merhaba dünya", "Ayşe");
echo $p->title . " - " . $p->author;

Kendini test et

  1. Yapıcı metot neden kullanışlı?

    • Sınıf adını gizlemek için
    • Örnek oluştururken veriyi tek seferde verip property'lere atamak için
    • Dizi öğelerini silmek için
    • Değişken türünü değiştirmek için
    Cevabı göster

    Örnek oluştururken veriyi tek seferde verip property'lere atamak için — Doğru. Yapıcı metot, new ile örnek oluşturulurken veriyi parametre olarak alıp property'lere atar; örnek tek satırda hazır olur.

  2. Yapıcı metoda parametre listesi ekle

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

    __construct
    Yapıcı metot adı
    $this->title
    Property ataması
    string $title
    Başlık parametresi
    new Post(...)
    Örnek oluşturma

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