What is the difference between Classes and Interfaces?

Asked

Viewed 930 times

5

What’s the difference between classes and interfaces?

I’ve read that apparently the difference in interfaces is because they are 100% public, but I have no idea.

Could someone explain the difference between them?

  • 1

    I suggest you take a look at this question http://answall.com/questions/2913/em-orientates%C3%A7%C3%A3o-a-objetos-por-que-interfaces-s%C3%A3o-%C3%Bateis. There’s a lot about interfaces there and there’s even an answer using PHP. This can help you get a better idea of what interfaces are and why they are different from classes.

  • 1

    In short, interface is a class where a contract is defined which methods should exist in the class that uses such an interface.

1 answer

7


All Interface is a class, but not all class is an interface, the use of interfaces in PHP is similar to other programming languages.

A base interface serves to define some standards that the class that will implement it must have. Generally we use to ensure a certain behavior of the other classes that will implement a certain Interface.

The methods of an interface are 100% public as you mentioned, because you will never execute one interface, she serves as a skeleton to another class, that is to determine methods "usable patterns" by the object, it would not make sense to declare something private or protected on the interface, since this would only run "internally".

Classes

In PHP5 are the inclusion of visibility, classes and methods Abstract and final, magical methods additional, interfaces (as I mentioned earlier) and cloning.

PHP treats objects in the same way as references or manipulators, meaning that each variable contains a reference to an object rather than a copy of the entire object. See Objects and References

Interfaces de Objetos

Object Interfaces allows code creation that specifies which methods and variables a class should implement, without having to define how these methods will be treated.

Interfaces are defined using the keyword interface in the same way as a common class, but without any of the methods having their content defined.

All methods declared in an interface must be public. This is the nature of an interface.

  • Implements

    To implement an interface, the Implements operator is used. All methods in the interface must be implemented in the class; not doing so will result in a fatal error. Classes can implement more than one interface if desired, separating each interface with a comma.

  • Example:

    • Using an Interface

      <?php
      
      // Declara a interface 'iTemplate'
      interface iTemplate
      {
          public function setVariable($name, $var);
          public function getHtml($template);
      }
      
      // Implementa a interface
      // Isso funcionará
      class Template implements iTemplate
      {
          private $vars = array();
      
          public function setVariable($name, $var)
          {
              $this->vars[$name] = $var;
          }
      
          public function getHtml($template)
          {
              foreach($this->vars as $name => $value) {
                  $template = str_replace('{' . $name . '}', $value, $template);
              }
      
              return $template;
          }
      }
      
      // Isso NÃO funcionará
      // Fatal error: Class BadTemplate contains 1 abstract methods
      // and must therefore be declared abstract (iTemplate::getHtml)
      class BadTemplate implements iTemplate
      {
          private $vars = array();
      
          public function setVariable($name, $var)
          {
              $this->vars[$name] = $var;
          }
      }
      
    • Extensible interfaces

      <?php
      interface a
      {
          public function foo();
      }
      
      interface b extends a
      {
          public function baz(Baz $baz);
      }
      
      // Isto irá funcionar
      class c implements b
      {
          public function foo()
          {
          }
      
          public function baz(Baz $baz)
          {
          }
      }
      
      // Isto não irá funcionar e resultará em um erro fatal
      class d implements b
      {
          public function foo()
          {
          }
      
          public function baz(Foo $foo)
          {
          }
      }
      
    • Interface with multiple inheritance

      <?php
      interface a
      {
          public function foo();
      }
      
      interface b
      {
          public function bar();
      }
      
      interface c extends a, b
      {
          public function baz();
      }
      
      class d implements c
      {
          public function foo()
          {
          }
      
          public function bar()
          {
          }
      
          public function baz()
          {
          }
      }
      
    • Interfaces with constants

      <?php
      interface a
      {
          const b = 'Interface constant';
      }
      
      // Imprime: Interface constant
      echo a::b;
      
      
      // Isto não funcionará porque não é permitido
      // sobreescrever constantes.
      class b implements a
      {
          const b = 'Class constant';
      }
      

Note:

Until PHP 5.3.9, a class could not implement two interfaces that specify a method with the same name, as this would cause ambiguity. Newer versions allow this as long as duplicate methods have the same signature.

Interfaces can be extended as classes, using the extends operator.

The class that implements the interface needs to have the same method signatures as those defined in the interface. Otherwise a fatal error will be released.

Source: http://php.net/manual/en/language.oop5.interfaces.php

Browser other questions tagged

You are not signed in. Login or sign up in order to post.