Skip to content

Instantly share code, notes, and snippets.

@Dyrits
Forked from codecademydev/index.php
Last active May 10, 2023 08:06
Show Gist options
  • Save Dyrits/f7d513fef403b4d53934adde04e68e4d to your computer and use it in GitHub Desktop.
Save Dyrits/f7d513fef403b4d53934adde04e68e4d to your computer and use it in GitHub Desktop.
Going to Bed

Going to Bed

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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment