why cannot I declare an abstract method within an interface? This is my code. Thank you.
<?php
interface Connection {
public abstract function connect();
public function getConnection();
}
abstract class ConnectionAbstract implements Connection() {
private $connection;
public abstract function connect();
public function getConnection() {
return $this->connection;
}
}
class MySQLConnection extends ConnectionAbstract {
public function connect() {
echo 'connecting ...';
}
}
$c = new MySQLConnection();
?>
All functions in an interface are implicitly abstract. There is no need to use the abstract keyword when declaring the function.
Remember that the requirement of a class which implements an interface must contain a series of public methods which correspond to the method signatures declared in the interface. So, for instance, when you declare an interface which has a defined public abstract function
, you're literally saying that every class which implements the interface must have a public abstract method named connect
. Since objects with abstract methods cannot be instantiated, you'll end up writing an interface which can never be used.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With