What is the difference between abstract classes and interfaces in PHP?
Abstract classes in PHP can contain both abstract and non-abstract methods, while interfaces can only contain method signatures without any implementation. Abstract classes can have properties, while interfaces cannot. Abstract classes can provide a partial implementation of a class, while interfaces define a contract that classes must adhere to.
<?php
// Abstract class
abstract class Animal {
protected $name;
public function setName($name) {
$this->name = $name;
}
abstract public function makeSound();
}
// Interface
interface Vehicle {
public function start();
public function stop();
}
?>
Related Questions
- How can users effectively copy and paste pre-written texts in PHP websites without encountering errors?
- What are the advantages and disadvantages of using a foreach loop to find the largest value in an array in PHP?
- How can firewall settings impact PHP's ability to connect to external servers, especially when dealing with ports like 587 and 465 for SSL connections?