Domain Specific Languages
I am Troels Knak-Nielsen. From Copenhagen. Have worked with web and PHP for about a decade.
Concepts Examples Perspective Questions
<?php class PurchaseHandler { protected $customer; function __construct(Customer $customer) { $this->customer = $customer;...
when customer purchase (car) withdraw (priceof car) from (customer account) Domain Specific Language
 
Styles
Language Pipeline Grammar Parser Internal Representation Runtime Environment
&quot;&quot;Some people, when confronted with a problem, think &quot;I know, I'll use regular expressions.&quot; Now they ...
when customer purchase (car) withdraw (priceof car) from (customer account) External style
when(customer(purchase(CAR)), withdraw(priceof(CAR))->from(customer(ACCOUNT))); Internal style
Concepts Examples Perspective Questions
Method Chaining
$select = $db->select() ->from('products') ->where('price > 4'); Zend_Db_Select
$bowling = new Bowling; describe($bowling)-> score->should()->be(0)-> when('When bowling has not yet started'); describe($...
Global Symbols
when(customer(purchase(CAR)), withdraw(priceof(CAR))->from(customer(ACCOUNT))); The car-salesman
Lambda/ Scope
with('request')->begin(function($request) { $request->isParameter('module', 'foo'); $request->isParameter('action', 'index...
$browser-> get('/')-> with('request')->begin()-> isParameter('module', 'foo')-> isParameter('action', 'index')-> end()-> c...
Monadic Style
$customer_ids = array('22731', '2458', '14442'); snapshot( 'bg_account_detail', 'bg_adjustment', where('account_id', selec...
Macros
stream_wrapper_register Allows you to implement your own protocol handlers and streams for use with all the other filesyst...
Hybrid External/ Internal
pq($li)->addClass('my-second-new-class'); .......................................................................... $q = ...
Concepts Examples Perspective Questions
Why Bother?
Language Design
&quot;&quot;With great power comes great responsibility.&quot;&quot; -- Uncle Ben
Questions?
Follow me on  @troelskn
Upcoming SlideShare
Loading in …5
×

An overview of Domain Specific Languages in PHP

6,650 views

Published on

Slides from my talk on Domain Specific Languages at PF-Congress, 2010.

Original synopsis:

While external Domain Specific Languages has always been a natural part of PHP development, the recent awareness of the internal DSL style of programming has been relatively slow to enter the PHP world.

In this talk I'll give a short introduction to DSL's and the difference between the external and internal style. I'll then focus on the internal DSL style and show some common techniques and patterns used in the PHP world, giving examples as we move along.

Published in: Technology
0 Comments
8 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
6,650
On SlideShare
0
From Embeds
0
Number of Embeds
170
Actions
Shares
0
Downloads
32
Comments
0
Likes
8
Embeds 0
No embeds

No notes for slide

An overview of Domain Specific Languages in PHP

  1. 1. Domain Specific Languages
  2. 2. I am Troels Knak-Nielsen. From Copenhagen. Have worked with web and PHP for about a decade.
  3. 3. Concepts Examples Perspective Questions
  4. 4. <?php class PurchaseHandler { protected $customer; function __construct(Customer $customer) { $this->customer = $customer; } function register(EventDispatcher $event_dispatcher) { $event_dispatcher->registerOnPurchase( PurchaseType::CAR, $customer, array($this, 'onPurchase')); } function onPurchase(Car $car) { $account = $this->customer->getAccount(); $account->withdraw($car->getPrice()); } } General Purpose Language (PHP)
  5. 5. when customer purchase (car) withdraw (priceof car) from (customer account) Domain Specific Language
  6. 7. Styles
  7. 8. Language Pipeline Grammar Parser Internal Representation Runtime Environment
  8. 9. &quot;&quot;Some people, when confronted with a problem, think &quot;I know, I'll use regular expressions.&quot; Now they have two problems.&quot;&quot; -- Jamie Zawinski
  9. 10. when customer purchase (car) withdraw (priceof car) from (customer account) External style
  10. 11. when(customer(purchase(CAR)), withdraw(priceof(CAR))->from(customer(ACCOUNT))); Internal style
  11. 12. Concepts Examples Perspective Questions
  12. 13. Method Chaining
  13. 14. $select = $db->select() ->from('products') ->where('price > 4'); Zend_Db_Select
  14. 15. $bowling = new Bowling; describe($bowling)-> score->should()->be(0)-> when('When bowling has not yet started'); describe($bowling)-> score->shouldNot()->beGreaterThan(0)-> when('When bowling has not yet started'); PhpSpec
  15. 16. Global Symbols
  16. 17. when(customer(purchase(CAR)), withdraw(priceof(CAR))->from(customer(ACCOUNT))); The car-salesman
  17. 18. Lambda/ Scope
  18. 19. with('request')->begin(function($request) { $request->isParameter('module', 'foo'); $request->isParameter('action', 'index'); });
  19. 20. $browser-> get('/')-> with('request')->begin()-> isParameter('module', 'foo')-> isParameter('action', 'index')-> end()-> checkResponseElement('h1', 'foo') ; Symfony sfBrowser
  20. 21. Monadic Style
  21. 22. $customer_ids = array('22731', '2458', '14442'); snapshot( 'bg_account_detail', 'bg_adjustment', where('account_id', select('account_id', 'bg_account', where('customer_id', $customer_ids)))); .......................................................................... function select($column, $table, $condition = null) { return new Selection($column, $table, $condition); } function where($column, $value) { return new Condition($column, $value); }
  22. 23. Macros
  23. 24. stream_wrapper_register Allows you to implement your own protocol handlers and streams for use with all the other filesystem functions (such as fopen(), fread() etc.).
  24. 25. Hybrid External/ Internal
  25. 26. pq($li)->addClass('my-second-new-class'); .......................................................................... $q = Doctrine_Query::create() ->select('u.id') ->from('User u'); phpQuery Doctrine
  26. 27. Concepts Examples Perspective Questions
  27. 28. Why Bother?
  28. 29. Language Design
  29. 30. &quot;&quot;With great power comes great responsibility.&quot;&quot; -- Uncle Ben
  30. 31. Questions?
  31. 32. Follow me on @troelskn

×