Skip to content

Instantly share code, notes, and snippets.

@HoundstoothSTL
Created June 1, 2013 02:48
Show Gist options
  • 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' => '[email protected]'
],
'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' => '[email protected]'
]);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment