Skip to content

Instantly share code, notes, and snippets.

@facine
Forked from e0ipso/magic.php
Created October 8, 2013 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save facine/6883191 to your computer and use it in GitHub Desktop.
Save facine/6883191 to your computer and use it in GitHub Desktop.
<?php
/**
* A base class with getters and setters incorporated.
*/
class ObjectBase {
/**
* Setter function for a generic attribute.
*
* @param string $property_name
* Name of the attribute to set.
* @param mixed $value
* The value to set.
*/
public function set($attribute_name, $value) {
$this->{$attribute_name} = $value;
}
/**
* Getter for a generic attribute.
*
* @param string $attribute_name
* The name of the attribute to get.
* @return mixed
* The value of the attribute.
*/
public function get($attribute_name) {
return $this->{$attribute_name};
}
/**
* Get a dash based attribute name based on a camel cased name.
*
* @param string $camel
* The camel case name.
* @return string
* The dash separated name.
*/
private function getAttribute($camel) {
// Make the first letter lower case.
$camel[0] = strtolower($camel[0]);
// Insert a dash in front of every uppercase character.
$attribute = preg_replace('/([A-Z0-9])/', "_$1", $camel);
// Make everything lower case.
return strtolower($attribute);
}
/**
* Implement the magic __call method to cath calls to undefined methods.
*/
function __call($method, $args) {
// Detect if it's a getter
if (strpos($method, 'get') === 0 && count($args) == 0) {
$attribute_name = $this->getAttribute(substr($method, 3));
return $this->get($attribute_name);
}
elseif (strpos($method, 'set') === 0 && count($args) == 1) {
$attribute_name = $this->getAttribute(substr($method, 3));
$this->set($attribute_name, $args[0]);
}
else {
// Raise an error because we don't know the method.
throw new ErrorException("Undefined method: $method");
}
}
}
/**
* Bank account dummy class to demonstrate the use of the ObjectBase class.
*/
class BankAccount extends ObjectBase {
/**
* Balance for this account.
*/
protected $balance;
/**
* Number of credit cards registered to this account.
*/
protected $credit_cards;
/**
* Number of transactions to display.
*/
public $transactions;
function __construct() {
$this->balance = 0;
$this->transactions = 0;
$this->credit_cards = 0;
}
}
$account = new BankAccount();
// Set some values.
$account->setBalance(1000);
$account->setCreditCards(2);
$account->setTransactions(25);
// Get some values.
print "Balance : " . $account->getBalance() . "\n";
print "Transactions: " . $account->getTransactions() . "\n";
print "Credit Cards: " . $account->getCreditCards() . "\n";
/**************************************/
// Simple inheritance? No problem, do it with traits!
trait ObjectTrait {
/**
* Setter function for a generic attribute.
*
* @param string $property_name
* Name of the attribute to set.
* @param mixed $value
* The value to set.
*/
public function set($attribute_name, $value) {
$this->{$attribute_name} = $value;
}
/**
* Getter for a generic attribute.
*
* @param string $attribute_name
* The name of the attribute to get.
* @return mixed
* The value of the attribute.
*/
public function get($attribute_name) {
return $this->{$attribute_name};
}
/**
* Get a dash based attribute name based on a camel cased name.
*
* @param string $camel
* The camel case name.
* @return string
* The dash separated name.
*/
private function getAttribute($camel) {
// Make the first letter lower case.
$camel[0] = strtolower($camel[0]);
// Insert a dash in front of every uppercase character.
$attribute = preg_replace('/([A-Z0-9])/', "_$1", $camel);
// Make everything lower case.
return strtolower($attribute);
}
/**
* Implement the magic __call method to cath calls to undefined methods.
*/
function __call($method, $args) {
// Detect if it's a getter
if (strpos($method, 'get') === 0 && count($args) == 0) {
$attribute_name = $this->getAttribute(substr($method, 3));
return $this->get($attribute_name);
}
elseif (strpos($method, 'set') === 0 && count($args) == 1) {
$attribute_name = $this->getAttribute(substr($method, 3));
$this->set($attribute_name, $args[0]);
}
else {
// Raise an error because we don't know the method.
throw new ErrorException("Undefined method: $method");
}
}
}
class BankAccountTraits {
use ObjectTrait;
/**
* Balance for this account.
*/
protected $balance;
/**
* Number of credit cards registered to this account.
*/
protected $credit_cards;
/**
* Number of transactions to display.
*/
public $transactions;
function __construct() {
$this->balance = 0;
$this->transactions = 0;
$this->credit_cards = 0;
}
}
$account = new BankAccountTraits();
// Set some values.
$account->setBalance(1000);
$account->setCreditCards(2);
$account->setTransactions(25);
// Get some values.
print "Balance : " . $account->getBalance() . "\n";
print "Transactions: " . $account->getTransactions() . "\n";
print "Credit Cards: " . $account->getCreditCards() . "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment