Skip to content

Instantly share code, notes, and snippets.

@gusrub
Created April 26, 2018 04:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gusrub/06a194bfa2c4ecd6339426a1c1d64a0c to your computer and use it in GitHub Desktop.
Save gusrub/06a194bfa2c4ecd6339426a1c1d64a0c to your computer and use it in GitHub Desktop.
Encapsulating translation
<?php
require_once('i18n.php');
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(
'John',
'Wayne',
'1980-01-20'
);
$p->printInfo();
$p->goodBye();
$p->printVersion();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment