-
-
Save GDXbsv/d1a729e6a291ba4eb22c643da832b8fc to your computer and use it in GitHub Desktop.
Revisions
-
Ocramius revised this gist
May 20, 2015 . 1 changed file with 5 additions and 4 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 @@ -12,7 +12,7 @@ Mainly: - `function` is not testable in isolation if it isn't a pure function without dependencies In order to _fix_ the problems exposed above, and avoid global or static scope pollution, two approaches are possible: - using a [`Closure`](http://php.net/manual/en/functions.anonymous.php), combined @@ -24,9 +24,10 @@ Few notes: - a class that only implements `__invoke` and `__construct` is basically a `function` combined with a `functor` - a class can be easily tested in isolation - a `Closure` with dependencies cannot be tested in isolation - in order to make a `Closure` testable in isolation, it must be combined with a **higher order function** - there is no runtime difference between using a `Closure` and any object with an `__invoke` method defined in its class -
Ocramius revised this gist
May 20, 2015 . 3 changed files with 4 additions and 6 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 @@ -19,8 +19,6 @@ public function __invoke(User $user) } } // usage (PHP 7) - see http://3v4l.org/kPnLM: $session = (new Login($sessionRepository))($user); 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 @@ -17,4 +17,4 @@ // usage (PHP 7) - see http://3v4l.org/kPnLM: $session = $loginFactory(sessionRepository)($user); 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 @@ -14,4 +14,4 @@ function login(User $user) // usage: $session = login($user); -
Ocramius revised this gist
May 20, 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 @@ -9,7 +9,7 @@ Mainly: - `function` cannot (should not) be used when side-effects occur - `function` does not work with dependency injection - `function` is not testable in isolation if it isn't a pure function without dependencies In order to "fix" the problems exposed above, and avoid global or static -
Ocramius revised this gist
May 20, 2015 . 1 changed file with 3 additions and 0 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 @@ -30,6 +30,9 @@ Few notes: with a **higher order function** - there is no runtime difference between using a `Closure` and any object with an `__invoke` method defined in its class - in PHP, `$a = function () use (...) {}` simply means "call the constructor of class `Closure` with these parameters and this body for `__invoke`", so you end up with an object that implements `__invoke` anyway. Therefore, `object` + `__invoke` is much simpler and flexible than using a `Closure`, whereas a `function` is not applicable in any context that -
Ocramius revised this gist
May 20, 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 @@ -12,7 +12,7 @@ Mainly: - `function` is not testable in insulation if it isn't a pure function without dependencies In order to "fix" the problems exposed above, and avoid global or static scope pollution, two approaches are possible: - using a [`Closure`](http://php.net/manual/en/functions.anonymous.php), combined -
Ocramius revised this gist
May 20, 2015 . 1 changed file with 36 additions and 0 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 @@ -0,0 +1,36 @@ # `__invoke` VS `function` VS `Closure` This gist is about: - https://twitter.com/taylorotwell/status/600680897550098432 - https://twitter.com/Ocramius/status/600705252539691008 Mainly: - `function` cannot (should not) be used when side-effects occur - `function` does not work with dependency injection - `function` is not testable in insulation if it isn't a pure function without dependencies In order to "fix" the problems designed above, and avoid global or static scope pollution, two approaches are possible: - using a [`Closure`](http://php.net/manual/en/functions.anonymous.php), combined with a [higher order function](http://en.wikipedia.org/wiki/Higher-order_function) (also called **functor**) - using an `object` that implements the [`__invoke`](http://php.net/manual/en/language.oop5.magic.php#object.invoke) magic method Few notes: - a class that only implements `__invoke` and `__construct` is basically a `function` combined with a `functor` - a class can be easily tested in insulation - a `Closure` with dependencies cannot be tested in insulation - in order to make a `Closure` testable in insulation, it must be combined with a **higher order function** - there is no runtime difference between using a `Closure` and any object with an `__invoke` method defined in its class Therefore, `object` + `__invoke` is much simpler and flexible than using a `Closure`, whereas a `function` is not applicable in any context that requires the usage of [DI](http://en.wikipedia.org/wiki/Dependency_injection). -
Ocramius revised this gist
May 20, 2015 . 1 changed file with 20 additions and 0 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 @@ -0,0 +1,20 @@ <?php // higher order function / factory - you name it $loginFactory = function (SessionRepository $sessions) { return function (User $user) use ($sessions) { // argh! (note that a static registry/locator isn't any better) global $sessions; $session = new Session($user); $sessions->add($session); return $session; }; }; // usage (PHP 7) - see http://3v4l.org/kPnLM: $loginFactory(sessionRepository)($user); -
Ocramius revised this gist
May 20, 2015 . 1 changed file with 17 additions and 0 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 @@ -0,0 +1,17 @@ <?php function login(User $user) { // argh! (note that a static registry/locator isn't any better) global $sessions; $session = new Session($user); $sessions->add($session); return $session; } // usage: login($user); -
Ocramius created this gist
May 20, 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,26 @@ <?php final class Login { private $sessions; public function __construct(SessionRepository $sessions) { $this->sessions = $sessions; } public function __invoke(User $user) { $session = new Session($user); $this->sessions->add($session); return $session; } } // usage: $login = new Login($sessionRepository); $login($user);