Skip to content

Instantly share code, notes, and snippets.

@feelinc
Last active August 29, 2015 13:57
Show Gist options
  • Save feelinc/9427439 to your computer and use it in GitHub Desktop.
Save feelinc/9427439 to your computer and use it in GitHub Desktop.
<?php
class SomeClass
{
/**
* Hold options data for later use.
*
* @var Array
*/
protected $options;
/**
* Return a option data.
*
* @var Option
*/
public function getOption($key)
{
if ( ! array_key_exists($key, $this->options)) {
$this->options[$key] = Option::where('key', '=', $key)->first();
}
return $this->options[$key];
}
/**
* The first process.
*
* @return void
*/
public function processFirst()
{
$firstOption = $this->getOption('the_first_key');
// Do something
}
/**
* The second process.
*
* @return void
*/
public function processSecond()
{
$secondOption = $this->getOption('the_second_key');
// Do something
}
/**
* The third process.
*
* @return void
*/
public function processThird()
{
$firstOption = $this->getOption('the_first_key');
// Do something
}
}
$aClass = new SomeClass();
// Call the first process
// This process will fetch "the_first_key" option from DB
$aClass->processFirst();
// Call the second process
// This process will fetch "the_second_key" option from DB
$aClass->processSecond();
// Call the third process
// This process will fetch "the_first_key" option cached in 'options' variable
$aClass->processThird();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment