-
-
Save codecademydev/f2e471ae6b2968e52f353acbdf45fe27 to your computer and use it in GitHub Desktop.
Codecademy export
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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