CodeBlanks Logo Icon CodeBlanks

Metot ekleme

PHP kursu · Orta seviye · Ders 21/115

Sınıfa ekleme, çıkarma ve arama methodlarını eklersin.

Koleksiyona metot ekleme

Sınıfın iş yapmasını metotlar sağlar. Collection sınıfına üç temel metot ekleyeceksin: add yeni öğe ekler, remove öğe siler, find öğenin konumunu döndürür.

Her metot $this üzerinden iç diziye ulaşır. Ekleme için $this->items[] = $item;, arama için array_search, silme için unset kullanırsın. Bunların hepsi önceki derslerde öğrendiğin dizi fonksiyonlarıdır.

Metot zincirleme (chaining)

Bir metot return $this; döndürdüğünde, dönen değer yine aynı örnektir. Böylece metotları arka arkaya çağırabilirsin: $c->add("armut")->add("kiraz");.

Buna zincirleme (chaining) denir. add ve remove metotlarında return $this; yazarak akıcı bir kullanım kazanırsın. find ise bir konum döndürdüğü için zincirleme için kullanılmaz.

Uygulama add, remove ve find metotlarını ekle

Collection sınıfına üç metot ekle. add(string $item): $this->items[] = $item; sonra return $this;. remove(string $item): array_search ile bul, unset ile sil, return $this;. find(string $item): array_search ile bul; false ise null, değilse konumu döndür. Sonra $c = new Collection(["elma"]); ile başla, $c->add("armut")->add("kiraz"); ile zincirle, $c->remove("elma"); ile sil ve echo count($c->items); ile sayıyı yazdır.

Örnek çözümü göster
$this->items[] = $item; return $this;. remove: array_search ile bul, unset ile sil, return $this;. find: array_search ile bul, false ise null değilse konumu döndür. new Collection(["elma"]) ile başla, $c->add("armut")->add("kiraz"); ile zincirle, $c->remove("elma"); ile sil.
Örnek çözüm:
<?php
// Collection sınıfının iskeleti
class Collection {
    public array $items = [];

    public function __construct(array $initial = []) {
        $this->items = $initial;
    }

    public function add(string $item) {
        $this->items[] = $item;
        return $this;
    }

    public function remove(string $item) {
        $key = array_search($item, $this->items, true);
        if ($key !== false) {
            unset($this->items[$key]);
        }
        return $this;
    }

    public function find(string $item) {
        $key = array_search($item, $this->items, true);
        return $key === false ? null : $key;
    }
}

$c = new Collection(["elma"]);
$c->add("armut")->add("kiraz");
$c->remove("elma");
echo count($c->items);

Kendini test et

  1. Metot zincirleme (chaining) ne gerektirir?

    • Metotların static olarak tanımlanmasını
    • Metotların $this döndürmesini
    • Sınıfın miras alınmasını
    • Property'lerin public olmasını
    Cevabı göster

    Metotların $this döndürmesini — Doğru. Bir metot $this döndürürse sonraki metot doğrudan onunla çağrılabilir: $c->add("x")->add("y").

  2. add metodu $this döndürsün

    public function add(string $item) {
        $this->items[] = $item;
        return ____
    }
    Cevabı göster
    public function add(string $item) {
        $this->items[] = $item;
        return $this;
    }
  3. Metot ve zincirleme kavramlarını eşleştir

    return $this;
    Zincirleme sağlar
    $this->items[]
    Diziye ekleme
    array_search
    Öğe arama
    unset
    Öğe silme

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