Last active
April 16, 2018 07:21
-
-
Save gusrub/d8cd63312f9e4db0df2c99d8daab44b5 to your computer and use it in GitHub Desktop.
Internationalization PHP 1
This file contains 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 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 sprintf(_("Hello my name is %s %s and I'm %s\n"), $this->firstName, $this->lastName, $age); | |
} | |
public function goodBye() | |
{ | |
echo _("That's it, good bye!\n"); | |
} | |
public function printVersion() | |
{ | |
echo sprintf("\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 = sprintf(_("%s years which means I'm a teen"), $age); | |
} else { | |
$response = sprintf(_("%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