Skip to content

Instantly share code, notes, and snippets.

@HoundstoothSTL
Created June 1, 2013 02:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HoundstoothSTL/5689120 to your computer and use it in GitHub Desktop.
Save HoundstoothSTL/5689120 to your computer and use it in GitHub Desktop.
PHP Object Literal Pattern
<?php
/**********
***** PHP Object Literal Pattern
***********/
$object = (object) [
'config' => (object) [
'username' => 'Rob Bennet',
'email' => 'rob@madebyhoundstooth.com'
],
'hello' => function() use(&$object) {
return "Hello " . $object->config->username . ". ";
},
'emailDisplay' => function() use(&$object) {
return "Your email address is " . $object->config->email;
},
'init' => function($options) use(&$object) {
$object->config = $options;
$doUsername = $object->hello;
$doEmail = $object->emailDisplay;
return $doUsername() . $doEmail();
}
];
$sayMyInfo = $object->init;
?>
<?=
$sayMyInfo((object) [
'username' => 'Logan',
'email' => 'wolverine@xmen.com'
]);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment