Created
August 15, 2011 11:51
-
-
Save sunaot/1146090 to your computer and use it in GitHub Desktop.
DSL in PHP sample. See Entity::customize method.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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