Start · Sprachen · PHP · Referenz · method_exists

method_exists

Funktion

Prüft, ob eine Klasse/ein Objekt eine bestimmte Methode hat.

seit PHP 4.0.0 Kategorie: oop

Signatur

method_exists(object|string $object_or_class, string $method): bool

Beschreibung

Ideal für Duck-Typing. Für Sichtbarkeitsprüfung: ReflectionMethod.

Parameter

Name Typ Default Beschreibung
$object_or_class Pflicht object|string Objekt oder Klasse.
$method Pflicht string Methode.

Rückgabewert

Typ
bool

Beispiele

Basis

<?php

if (method_exists($obj, 'toArray')) $data = $obj->toArray();