How can functions be categorized within classes to improve code organization and readability in PHP?
Functions can be categorized within classes by grouping related functions together based on their purpose or functionality. This helps improve code organization and readability by making it easier to locate and understand the functions that belong to a specific class. By organizing functions within classes, you can also take advantage of object-oriented programming principles such as encapsulation and inheritance.
class MathFunctions {
public function add($a, $b) {
return $a + $b;
}
public function subtract($a, $b) {
return $a - $b;
}
}
$math = new MathFunctions();
echo $math->add(5, 3); // Output: 8
echo $math->subtract(5, 3); // Output: 2