Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@sunaot
Created August 15, 2011 11:51
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 sunaot/1146090 to your computer and use it in GitHub Desktop.
Save sunaot/1146090 to your computer and use it in GitHub Desktop.
DSL in PHP sample. See Entity::customize method.
<?php
class HackObject
{
public $_entityName;
public $_attributeNames;
function entityName($val) { $func = $this->_entityName; $func($val); }
function attributeNames($val) { $func = $this->_attributeNames; $func($val); }
}
class EntityBase
{
static $entityName = 'entity_base';
static $attributeNames = array('attr','name');
static private function hacker() {
$hack = new HackObject;
$entity_name =& static::$entityName;
$hack->_entityName = function($val) use (&$entity_name) { $entity_name = $val; };
$attr_names =& static::$attributeNames;
$hack->_attributeNames = function($val) use (&$attr_names) { $attr_names = $val; };
return $hack;
}
static function createEntity() {
static::customize(static::hacker());
return new static;
}
}
class Entity extends EntityBase
{
static function customize($let) {
$let->entityName('another_name');
$let->attributeNames(array('another','attr','name'));
}
}
$ent = Entity::createEntity();
assert('another_name' === $ent::$entityName);
assert(array('another','attr','name') === $ent::$attributeNames );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment