Skip to content

Instantly share code, notes, and snippets.

@coreysan
Last active October 27, 2017 17:17
Show Gist options
  • Save coreysan/0befc7aabea7601ee1a933cb72c4124f to your computer and use it in GitHub Desktop.
Save coreysan/0befc7aabea7601ee1a933cb72c4124f to your computer and use it in GitHub Desktop.
PHP Junior Test: Person Refactor
<?php
/**
* A poor programmer has written the following code!
*
* It runs, but it's full of bad practices,
* doesn't leverage inheritance, and abuses
* Object-Oriented-Programming principles.
*
* Refactor it like you're writing open-source
* code for the world to see.
*
* Follow any best practices you know of.
*
* Run this file on the command line using:
* $ php person.php
*
* Or use a repl like https://repl.it/languages/php
*/
class Person {
public $name;
public $dob;
public $nn;
public function __construct($name, $dob, $nn){
$this->name = $name;
$this->dob = $dob;
$this->nn = $nn;
}
public static function getLowercaseNickname($nn){ return strtolower($nn); }
public static function n($user){ return $user->name; }
}
class Employee {
public $name;
public $dob;
public $nn;
public $pos;
public static $curr_enum = 0;
public function __construct($name, $dob, $nn, $pos){
$this->name = $name;
$this->dob = $dob;
$this->nn = $nn;
$this->pos = $pos;
self::$curr_enum++;
}
public static function getLowercaseNickname($nn){ return strtolower($nn); }
public static function getNumEmployees(){ return self::$curr_enum; }
public static function n($user){ return $user->name; }
public function pos(){ return $this->pos; }
}
$p = new Person("Corey", "1980-12-01", "The Assassin");
echo "My name is ".Person::n($p)."\n";
$e1 = new Employee("Jamal", "2017-12-01", "The Bass", "CEO");
$e2 = new Employee("Robert", "1990-10-01", "Creative Clown", 'Designer');
$e3 = new Employee("Roy", "1994-06-01", "Scumbuckler", "CTO");
echo "I've added ".Employee::getNumEmployees()." employees\n";
echo "Employee one is named ".Person::n($e1)."\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment