Skip to content

Instantly share code, notes, and snippets.

Created Apr 26, 2018
What would you like to do?
Encapsulating translation
use function Helpers\I18n\translate;
class Person
const VERSION = '1.0.0';
public $firstName;
public $lastName;
public $dob;
function __construct($firstName, $lastName, $dob)
$this->firstName = $firstName;
$this->lastName = $lastName;
$this->dob = $dob;
public function printInfo()
$age = $this->calculateAge();
echo translate("Hello my name is %s %s and I'm %s\n", $this->firstName, $this->lastName, $age);
public function goodBye()
echo translate("That's it, good bye!\n");
public function printVersion()
echo translate("\n(running version %s)\n", self::VERSION);
private function calculateAge()
$dob = date_create($this->dob);
$now = date_create();
$age = date_diff($dob, $now)->format("%y");
$response = null;
if($age < 18) {
$response = translate("%s years which means I'm a teen", $age);
} else {
$response = translate("%s years which means I'm a grown up", $age);
return $response;
$p = new Person(
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment