How can functions and methods be utilized in PHP classes to streamline tasks like printing sentences in different colors?

To streamline tasks like printing sentences in different colors within PHP classes, functions and methods can be utilized. By creating a function that accepts the desired text and color as parameters, and then using this function within class methods, you can easily output colored text without repeating the styling code each time.

<?php

class ColorPrinter {
    public function printColoredText($text, $color) {
        echo "<span style='color: $color;'>$text</span>";
    }
}

$printer = new ColorPrinter();
$printer->printColoredText("This is a red text.", "red");
$printer->printColoredText("This is a blue text.", "blue");

?>