How to declare abstract methods on interface in OOP

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 it has the function (method) to increase volume, but for I increase the volume I need to receive a value from the keyboard to increase the volume, I'm using PHP

interface Controlador { 

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

Page of the class that implements the interface. I have only pasted the block with the function body, not the entire 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 Remote_Control :: increase_volume () must be compatible with Controller :: increase_volume () in C: \ xampp \ htdocs \ Programs_Orientacao_Obj \ Remote_Control \ Remote_Controller.php on line 4

    
asked by anonymous 07.12.2017 / 00:52

1 answer

1

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

In its interface, the aumentar_volume method has no arguments; then it would be like you own a door and put a warning: "you can come in, there is 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 aumentar_volume method you do not need to pass any value as a parameter, but when you implement the same method in the class, you expect a value. PHP understands this as an error.

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

Considering the second solution, your interface would look like:

interface Controlador { 

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

I have already added the argument in the diminuir_volume method, since it seems to have the same problem.

    
07.12.2017 / 01:08