How do I know if a class implements an interface?

8

In PHP, we can implement one (or more) interface in a class.

So:

  interface Authenticable {
       // métodos
  }

  interface Model {
      // métodos
  }

  class Person implements Authenticable, Model {
     // métodos
  }

In the example above, Person implements Authenticable and Model .

How can I make a check to see if a particular class implements an interface? For example, how do you know that a given instance of Person implements Authenticable ?

Note : I always use versions of PHP 5.4 > =. So a more current form would be preferable for such an operation.

    
asked by anonymous 21.10.2016 / 12:39

2 answers

9

The instanceof operator is used:

interface Authenticable {
     // métodos
}
interface Model {
    // métodos
}
class Person implements Authenticable, Model {
   // métodos
}
$pessoa = new Person();
if ($pessoa instanceof Model) {
    echo "implementa Model";
}
if ($pessoa instanceof Cloneable) {
    echo "implementa Cloneable";
}

See working on ideone and in PHP SandBox .

It's not what you want and in a few situations it's useful (I think only if you're doing some help), but if you prefer you can list everything :

interface Authenticable {
     // métodos
}
interface Model {
    // métodos
}
class Person implements Authenticable, Model {
   // métodos
}
$pessoa = new Person();
print_r(class_implements($pessoa));

See working on ideone and on CodingPad .

    
21.10.2016 / 12:50
1

To know which interfaces a class implements, simply use the SPL function class_implements ()

Scope

array class_implements ( mixed $class [, bool $autoload = true ] )

From PHP 5.1 you can set the class name to string:

$rs = class_implements('Foo');
var_dump($rs);

We can use instanceof as a more practical medium, but we need to make sure that the instance is an interface, otherwise it may create an inconsistency by returning extended classes rather than interfaces. What's more, with instanceof it is assumed that you already know the names of the interfaces. With class_implements() an array of names of the implemented objects is returned. It is most useful for when you do not know which objects are implemented.

These are the objects we will use for testing:

interface iFoo {
}

class Bar {
}

class Foo implements ifoo {
}

class Foo2 extends Bar {
}

Test with class_implements ()

$rs = class_implements('Foo');
var_dump($rs);
/*
array(1) {
  ["iFoo"]=>
  string(4) "iFoo"
}
*/

$rs = class_implements('Foo2');
var_dump($rs);
/*
Foo2 é uma instância de Bar, porém, não retorna ao invocar class_implements() por ser uma class e não uma interface. O resultado nesse caso é vazio.

array(0) {
}
*/

Test with instanceof

O uso do 'instanceof' pode causar inconsistência. Veja o exemplo:

$c = new Foo;
if ($c instanceof iFoo) {
    echo 'yes';
} else {
    echo 'no';
}
/*
iFoo é uma interface. O retorno é 'yes'.
*/

$c = new Foo2;
if ($c instanceof Bar) {
    echo 'yes';
} else {
    echo 'no';
}

/*
Bar() não é uma interface, mas ainda assim retorna 'yes' porque instanceof não distingue se o objeto é uma class ou uma interface.
Isso é um exemplo de inconsistência.
*/

It is good to make it clear that I do not mean that it is wrong to use instanceof for this purpose (within the context of the question), provided the caveats are observed.

However, it is good to point out the inconsistency when indicating the use of instanceof for this purpose. For security, use whichever is more consistent, the class_implements() function.

Reflection?

I would not even want to comment on the use of Reflection because for static tasks there is no sense in using OOP classes.

But if you already have an instance of ReflectionClass () , you can take advantage of it to get a list of the interfaces that the object implements:

$rs = new ReflectionClass('Foo');
var_dump($rs->getInterfaces());
/*
array(1) {
  ["iFoo"]=>
  object(ReflectionClass)#2 (1) {
    ["name"]=>
    string(4) "iFoo"
  }
}
*/

Reference: link

Note: The examples above show only how to abstract the data. I do not need to show how to use if else , in_array() and stuff like that to get the name of an object that might be present in the result. After all, anyone who is reading this should know how to use such basic things.

    
25.10.2016 / 14:11