Very good,
As an exercise I am trying to create an interface that inherits from another. When implementing it in the class, it is giving me the error that the methods of the class are not compatible with those of the interface.
I don't understand why, can someone explain it?
Thank you
<?php
interface Auto{
function encender();
function apagar();
}
interface Gasolina extends Auto{
function vaciarTanque();
function llenarTanque($x);
}
class Deportivo implements Gasolina{
private $tanque = 0;
private $estado = 'apagado';
function llenarTanque($x){
if($x > 50){
echo $this->tanque = 50;
}else{
echo $this->tanque = $x;
}
}
function vaciarTanque(){
$this->tanque = 0;
}
function encender(){
echo $this->estado = 'encendido';
}
function apagar(){
echo $this->estado = 'apagado';
}
function get_estado(){
return $this->estado;
}
function usarCoche($km){
if($this->tanque == 0){
echo 'El depósito está vacío, por favor, recárguelo';
}else{
if($this->estado == 'apagado'){
echo 'El coche está apagado';
}else{
$kms = $this->tanque * 7;
if($km > $kms){
echo 'No puedes hacer esa cantidad de kms';
}else{
$resto = $kms - $km;
if($resto == 0){
$this->tanque = $resto / 7;
echo "has logrado hacer $km kms, Su tanque está en 0. Por favor. Rellene su tanque";
}else{
echo "Has logrado hacer $km kms, y le quedan $this->tanque lts en el depósito";
}
}
}
}
}
function secuencia(){
llenarTanque(35);
echo get_estado();
encender();
echo get_estado();
usarCoche(100);
echo get_estado();
apagar();
}
}
$coche = new Deportivo;
$coche->secuencia();
The fillTank method does not use parameters, however, by implementing it you are using parameters. It can be fixed by adding a parameter to the interface method or by removing it from the implementation
Also, as indicated by @A. Cedano in comments, you should use the $this-> in calls you make to the sequence() method.
Your code can be as follows: