Last active
August 29, 2015 14:14
-
-
Save johnkary/344902d13e6d87fac6e8 to your computer and use it in GitHub Desktop.
Revisions
-
johnkary revised this gist
Feb 7, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -33,7 +33,7 @@ public function isLazy() { } namespace Mailer\Options { class Lazy { public function __invoke(\Mailer\Mailer $mailer) { $mailer->setLazy(true); } } -
johnkary revised this gist
Feb 7, 2015 . 1 changed file with 9 additions and 3 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -11,6 +11,7 @@ class Mailer { private $host; private $lazy = false; // ... is variadic function syntax. See: http://php.net/manual/en/functions.arguments.php#functions.variable-arg-list public function __construct($host, ...$options) { $this->host = $host; @@ -47,19 +48,24 @@ function SuperLazy() { namespace { use function \Mailer\Options\SuperLazy; // Anonymous functions configure Mailer // Wire up as many as needed for optional settings and pass as constructor arguments $tls = function (\Mailer\Mailer $mailer) { echo 'LOL Lets do some TLS --'; }; // Any one of these $lazy vars can be passed as options arguments // 1. Anonymous function $lazy1 = function (\Mailer\Mailer $mailer) { $mailer->setLazy(true); }; // 2. Object that responds to being used like a function via __invoke() $lazy2 = new \Mailer\Options\Lazy; // 3. Namespaced function (must return an anonymous function) $lazy3 = SuperLazy(); $mailer = new \Mailer\Mailer('www.gmail.com', $tls, $lazy3); // Add any function options to your liking echo $mailer->isLazy() ? 'Yes lazy' : 'No lazy'; } -
johnkary created this gist
Feb 7, 2015 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,65 @@ <?php /** * PHP 5.6 example of "Functional Options" as presented by Rob Pike * in his article "Self-referential functions and the design of options" * http://commandcenter.blogspot.com.au/2014/01/self-referential-functions-and-design.html */ namespace Mailer { class Mailer { private $host; private $lazy = false; public function __construct($host, ...$options) { $this->host = $host; foreach ($options as $option) { $option($this); } } // Wish this could be a private function and still be // invoked by an options function so it's not mutable from the outside public function setLazy($lazy) { $this->lazy = $lazy; } public function isLazy() { return $this->lazy; } } } namespace Mailer\Options { class Lazy { public function __invoke(Mailer $mailer) { $mailer->setLazy(true); } } function SuperLazy() { return function(\Mailer\Mailer $mailer) { $mailer->setLazy(true); }; } } namespace { use function \Mailer\Options\SuperLazy; $tls = function (\Mailer\Mailer $mailer) { echo 'LOL Lets do some TLS --'; }; // Any one of these $lazy vars can be passed as an option $lazy1 = function (\Mailer\Mailer $mailer) { $mailer->setLazy(true); }; $lazy2 = new \Mailer\Options\Lazy; $lazy3 = SuperLazy(); // Ughhh, I want to do `$lazy3 = SuperLazy;` but PHP won't let me $mailer = new \Mailer\Mailer('www.gmail.com', $tls, $lazy3); echo $mailer->isLazy() ? 'Yes lazy' : 'No lazy'; }