Fonksiyon method_exists

method_exists fonksiyonu, belirtilen metodun belirtilen sınıfta veya nesnede var olup olmadığını kontrol eder. Fonksiyon ilk parametre olarak bir nesne veya sınıf adı, ikinci parametre olarak da string türünde bir metod adı alır. Metot mevcutsa true, aksi takdirde false döndürür.

Sözdizimi

phpmethod_exists(object|string $class, string $method): bool

Örnek

Bir sınıfta metodun varlığını kontrol edelim:

php<?php
  class MyClass {
    public function test() {}
  }

  $res = method_exists('MyClass', 'test');
  var_dump($res);
?>

Kodun çalıştırılma sonucu:

phptrue

Örnek

Bir nesnede metodun varlığını kontrol edelim:

php<?php
  $obj = new MyClass();
  $res = method_exists($obj, 'test');
  var_dump($res);
?>

Kodun çalıştırılma sonucu:

phptrue

Örnek

Var olmayan bir metodu kontrol edelim:

php<?php
  $res = method_exists('MyClass', 'notExists');
  var_dump($res);
?>

Kodun çalıştırılma sonucu:

phpfalse

Ayrıca Bakınız