This project provides practice for:
- Creating objects from classes
- Writing and using methods
- Using static methods
| <?php | |
| class StringUtils | |
| { | |
| public static function secondCase(string $text) { | |
| $text = strtolower($text); | |
| if (strlen($text) > 1) { | |
| $text[1] = strtoupper($text[1]); | |
| } | |
| return $text; | |
| } | |
| } | |
| class Pajamas | |
| { | |
| private $owner, $fit, $color; | |
| function __construct( | |
| string $owner = "unknown", | |
| string $fit = "fine", | |
| string $color = "white" | |
| ) { | |
| $this->owner = StringUtils::secondCase($owner); | |
| $this->fit = $fit; | |
| $this->color = $color; | |
| } | |
| public function describe() { | |
| return "This pajama belongs to $this->owner. It fits $this->fit and is $this->color."; | |
| } | |
| function setFit(string $fit) { | |
| $this->fit = $fit; | |
| } | |
| } | |
| class ButtonablePajamas extends Pajamas | |
| { | |
| private $button_state = "unbuttoned"; | |
| public function describe() { | |
| return parent::describe() . " They also have buttons which are currently $this->button_state."; | |
| } | |
| public function toggleButtons() { | |
| $this->button_state = ["buttoned" => "unbuttoned", "unbuttoned" => "buttoned"][$this->button_state]; | |
| } | |
| } | |
| $chicken_PJs = new Pajamas("CHICKEN", "correctly", "red"); | |
| echo $chicken_PJs->describe(); | |
| echo "\n"; | |
| $chicken_PJs->setFit("awfully"); | |
| echo $chicken_PJs->describe(); | |
| echo "\n"; | |
| $moose_PJs = new ButtonablePajamas("moose", "appropriately", "black"); | |
| echo $moose_PJs->describe(); | |
| $moose_PJs->toggleButtons(); | |
| echo "\n"; | |
| echo $moose_PJs->describe(); | |