What are the advantages and disadvantages of using class methods in PHP?
Class methods in PHP provide a way to encapsulate functionality within a class, making it easier to organize and maintain code. They also allow for inheritance and polymorphism, enabling code reuse and promoting a more object-oriented approach. However, using class methods can sometimes lead to tight coupling between classes, making it harder to change or extend functionality.
class Calculator {
public static function add($num1, $num2) {
return $num1 + $num2;
}
public static function subtract($num1, $num2) {
return $num1 - $num2;
}
}
// Example usage
echo Calculator::add(5, 3); // Output: 8
echo Calculator::subtract(10, 2); // Output: 8