What is the difference between Classes and Interfaces?

4

What is 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 anyone explain the difference between them?

    
asked by anonymous 28.02.2016 / 20:57

1 answer

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

An interface in base serves to define some standards that the class that will implement it must have. Usually we use to guarantee a certain behavior of the other classes that come to implement a certain Interface .

The methods of an interface are 100% public as you mentioned, because you will never run an interface , it serves > skeleton to another class, ie to determine methods "usable patterns" by the object, it would not make sense to declare something private or protected in the interface, since this would only run "internally" p>

Classes

In PHP5 are the visibility , classes and methods abstract and end , additional magic methods , interfaces (as I mentioned earlier) and cloning .

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

Object Interfaces

Object Interfaces allows you to create code that specifies which methods and variables a class should implement, without having to define how those methods will be handled.

Interfaces are defined using the interface keyword, 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;
          }
      }
      
    • Extendable 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)
          {
          }
      }
      
    • Multiple Inheritance Interface

      <?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. More recent 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 must have the same method signatures as those defined in the interface. Otherwise a fatal error will be thrown.

Source: link

    
28.02.2016 / 21:17