How to declare abstract methods in OOP interface

Asked

Viewed 20 times

0

I would like to know how to create a method in the interface of a program in object orientation, where this method receives parameters... Example I am creating a remote control object where has the function(method) of increasing volume, but for me to increase the volume I need to receive a value from the keyboard to increase the volume, I am using PHP

interface Controlador { 

    public function ligar();
    public function desligar();
    public function aumentar_volume();
    public function diminuir_volume();
    public function abrir_menu();
}

Class page that implements the interface. I put only the block with the function body, not the whole page.

public function aumentar_volume($valor){

       if($this->getLigado(true)){
           if($this->getVolume <= $this->getVolume_max){
               $this->setVolume($this->$getVolume() + $this->getValor);
           }else{
               echo "Volume já atingiu o volume máximo";
           }
       } 
    }

Fatal error: Declaration of Control_remote::aumentar_volume() must be compatible with Controlador::aumentar_volume() in C: xampp htdocs Programas_orientacao_obj Controle_remote Controle_remote.php on line 4

  • In the interface, you defined the method without arguments, but in the class you defined an argument. That’s the error. If the method has an argument, it must be present in the interface as well.

  • Thank you Anderson but how do I do it? can help me please, I’m not getting this argument on the interface.

  • As you tried to do?

  • This way: interface Controller { private $value; public Function on(); public Function off(); public Function increases_volume($value); public Function decreasr_volume(); public Function opens_menu(); }

  • Method argument is completely different from class attribute. Argument would be $valor in the method aumentar_volume.

  • This in the interface? or I put in the page that was implemented the interface?

  • I’ve got it Thank you.

Show 2 more comments

1 answer

1

When you use an interface, an analogy you can make is that the class becomes a black box and the outside world just sees the interface. That is, the interface will define how the outside world will see its class.

In its interface, the method aumentar_volume has no arguments; so it would be like you owning a door and putting a warning: "You can come in, there’s no key," but when someone tries to open it, it’s locked. That’s what your interface is doing: telling the outside world to call the method aumentar_volume you do not need to pass any value as parameter, but when you implement the same method in the class, you expect a value. PHP understands this as a mistake.

Of two, one: either your class implementation is wrong and the method should have no arguments; or the interface implementation is wrong and the method should have arguments.

Considering the second solution, your interface would be:

interface Controlador { 

    public function ligar();
    public function desligar();
    public function aumentar_volume($valor);
    public function diminuir_volume($valor);
    public function abrir_menu();
}

I’ve already added the argument to the method diminuir_volume, because it seems to go through the same problem.

  • Our very good explanation thanks for the help I managed to understand in a very clear and objective way, now ran my code. Thank you very much

Browser other questions tagged

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