Skip to content

Instantly share code, notes, and snippets.

@satishgumudavelli
Created July 3, 2018 05:30
Show Gist options
  • Save satishgumudavelli/20445525116c452fbab3fcf164f392a8 to your computer and use it in GitHub Desktop.
Save satishgumudavelli/20445525116c452fbab3fcf164f392a8 to your computer and use it in GitHub Desktop.
<?php
/* create class */
class Record {
/* record information will be held in here */
private $info;
/* constructor */
function Record($record_array) {
$this->info = $record_array;
}
/* dynamic function server */
function __call($method,$arguments) {
$meth = $this->from_camel_case(substr($method,3,strlen($method)-3));
return array_key_exists($meth,$this->info) ? $this->info[$meth] : false;
}
/* uncamelcaser: via http://www.paulferrett.com/2009/php-camel-case-functions/ */
function from_camel_case($str) {
$str[0] = strtolower($str[0]);
$func = create_function('$c', 'return "_" . strtolower($c[1]);');
return preg_replace_callback('/([A-Z])/', $func, $str);
}
}
///* usage */
//$Record = new Record(
// array(
// 'id' => 12,
// 'title' => 'Greatest Hits',
// 'description' => 'The greatest hits from the best band in the world!'
// )
//);
//
///* proof it works! */
//echo 'The ID is: '.$Record->getId(); // returns 12
//echo 'The Title is: '.$Record->getTitle(); // returns "Greatest Hits"
//echo 'The Description is: '.$Record->getDescription(); //returns "The greatest hits from the best band in the world!"
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment