Skip to content

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
PHP Interface Implementation Test
interface TestInterface
public function foo();
public function bar();
class TestClassA implements TestInterface
public function foo() { }
public function bar() { }
class TestClassB
public function foo() { }
public function bar() { }
$testClassA = new TestClassA();
$testClassB = new TestClassB();
/* is_a() Function
* ---------------
echo "is_a() on TestClassA:\t\t";
var_dump(is_a($testClassA, 'TestInterface'));
echo "is_a() on TestClassB:\t\t";
var_dump(is_a($testClassB, 'TestInterface'));
/* instanceof Operator
* -------------------
echo "instanceof on TestClassA:\t";
var_dump($testClassA instanceof TestInterface);
echo "instanceof on TestClassB:\t";
var_dump($testClassB instanceof TestInterface);
/* Reflection
* ----------
$reflection = new ReflectionClass('TestClassA');
echo "Reflection on TestClassA:\t";
$reflection = new ReflectionClass('TestClassB');
echo "Reflection on TestClassB:\t";
Shows the various ways that PHP code has of establishing whether a class implements an interface.
The TestInterface, TestClassA and TestClassB all have the same method signature but only TestClassA uses "implements" in its definition.
The script demonstrates that in every case, the "implements" part of the declaration is what is used to provide the return values for these assessments.
We can infer that when a class is used for the first time, the PHP runtime analyses the methods of the class and those of any interfaces it is supposed to implement and stores the result.
Output of the above:
is_a() on TestClassA: bool(true)
is_a() on TestClassB: bool(false)
instanceof on TestClassA: bool(true)
instanceof on TestClassB: bool(false)
Reflection on TestClassA: bool(true)
Reflection on TestClassB: bool(false)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.