Skip to content

Instantly share code, notes, and snippets.

@AmyStephen
Last active December 17, 2015 02:49
Show Gist options
  • Save AmyStephen/5538748 to your computer and use it in GitHub Desktop.
Save AmyStephen/5538748 to your computer and use it in GitHub Desktop.
Translations with Anonymous Function
<?php
namespace Molajo;
{
/** Translation Class */
class Language implements LanguageInterface
{
public function translate($key)
{
if ($key == 'h') {
return 'Hello';
} else {
return 'Goodbye';
}
}
}
/** Concrete Class */
class Test
{
protected $language;
public function __construct($language)
{
$this->language = $language;
}
public function t($key)
{
$t = $this->language;
return $t($key);
}
public function doThisThing()
{
$message = 'ABC ' . $this->t('h') . ' ZYZ.';
return $message;
}
}
/** Anonymous Function */
$t = function ($request) {
$class = 'Molajo\\Language';
$language = new $class();
$value = $language->translate($request);
return $value;
};
/** Pass in the Translation Function */
$class = 'Molajo\\Test';
$test = new $class($t);
$thing = $test->doThisThing();
echo $thing;
}
@AmyStephen
Copy link
Author

Excellent. Oh boy! There is some very big potential for interoperability. I've been hoping you might be thinking that way. =)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment