Last active
August 3, 2019 13:44
-
-
Save alan-ps/b08b719b698a1f98ba97d7fa3a1ef83d to your computer and use it in GitHub Desktop.
Revisions
-
alan-ps revised this gist
Aug 3, 2019 . 1 changed file with 4 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 @@ -67,9 +67,6 @@ $closure = function() use ($string) { }; $closure(); // Hello World! ``` ##### Early and Late Binding ```php @@ -129,4 +126,7 @@ print_r($test2); // Some another text (function() { echo 'Hello World!'; })(); ``` ##### Helpful links: * [RFC: Lambda functions and closures](https://wiki.php.net/rfc/closures) * [Difference between closure parameters and the use keyword](https://stackoverflow.com/questions/10692817/whats-the-difference-between-closure-parameters-and-the-use-keyword) -
alan-ps revised this gist
Aug 3, 2019 . 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 @@ -67,6 +67,9 @@ $closure = function() use ($string) { }; $closure(); // Hello World! ``` ##### Helpful links: * [RFC: Lambda functions and closures](https://wiki.php.net/rfc/closures) * [Difference between closure parameters and the use keyword](https://stackoverflow.com/questions/10692817/whats-the-difference-between-closure-parameters-and-the-use-keyword) ##### Early and Late Binding ```php -
alan-ps revised this gist
May 30, 2019 . 1 changed file with 8 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 @@ -119,3 +119,11 @@ $test2 = Closure::bind($testA->showText(), $testB); print_r($test1); // Some another text print_r($test2); // Some another text ``` ##### Self-Executing Closures ```php <?php // Displays 'Hello World!'. (function() { echo 'Hello World!'; })(); ``` -
alan-ps revised this gist
May 30, 2019 . 1 changed file with 26 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 @@ -93,3 +93,29 @@ $b = function() use (&$a) { $a = 'Hello World!'; $b(); // Hello World! ``` ##### Binding Closures to Scopes ```php <?php class test { protected $text = 'Default text'; public function showText() { return function () { return $this->text; }; } } class testA extends test {} class testB { protected $text = 'Some another text'; } $testA = new testA(); $testB = new testB(); $test1 = $testA->showText()->bindTo($testB); $test2 = Closure::bind($testA->showText(), $testB); print_r($test1); // Some another text print_r($test2); // Some another text ``` -
alan-ps revised this gist
May 29, 2019 . 1 changed file with 27 additions 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 @@ -66,4 +66,30 @@ $closure = function() use ($string) { echo $string; }; $closure(); // Hello World! ``` ##### Early and Late Binding ```php <?php $a = 'Some string'; // Early binding (default). $b = function() use ($a) { echo $a; }; $a = 'Hello World!'; $b(); // Some string ``` ```php <?php $a = 'Some string'; // Late binding (reference). $b = function() use (&$a) { echo $a; }; $a = 'Hello World!'; $b(); // Hello World! ``` -
alan-ps revised this gist
May 29, 2019 . 1 changed file with 7 additions 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 @@ -48,7 +48,7 @@ function myFunc(): void { sayWorld(); // Hello World! ``` ### Lambda and Closure ```php <?php @@ -60,4 +60,10 @@ $lambda = function($a, $b) { $lambda(2, 3); // 5 echo (int) is_callable($lambda); // 1 echo get_class($lambda); // Closure $string = 'Hello World!'; $closure = function() use ($string) { echo $string; }; $closure(); // Hello World! ``` -
alan-ps revised this gist
May 29, 2019 . 1 changed file with 14 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 @@ -46,4 +46,18 @@ function myFunc(): void { } sayWorld(); // Hello World! ``` ### Lambda ```php <?php $lambda = function($a, $b) { echo $a + $b; }; $lambda(2, 3); // 5 echo (int) is_callable($lambda); // 1 echo get_class($lambda); // Closure ``` -
alan-ps renamed this gist
May 29, 2019 . 1 changed file with 14 additions 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 @@ -4,7 +4,7 @@ <?php function myFunc(?MyObject $myObj) { echo 'Hello World!'; } myFunc(null); // this is allowed @@ -34,3 +34,16 @@ function getFullName(string $firstName, string $lastName): string { $name = getFullName('Serhii', 'Puchkovskyi'); echo gettype($name); // string ``` ### Return Void If the function is going to return NULL, we can specify that it will return "void": ```php <?php function myFunc(): void { echo 'Hello World!'; } sayWorld(); // Hello World! ``` -
alan-ps created this gist
May 29, 2019 .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 @@ ### Alternate Null Type Syntax ```php <?php function myFunc(?MyObject $myObj) { echo 'Hello world!'; } myFunc(null); // this is allowed myFunc(); // this produces a fatal error: Too few arguments ``` ### Variadics ```php <?php function myFunc($required, $optional = NULL, ...$variadicParams) { print_r($variadicParams); } myFunc(1, 2, 3, 4); // array([0] => 3, [1] => 4); ``` ### Return Type Declarations ```php <?php function getFullName(string $firstName, string $lastName): string { return 100; } $name = getFullName('Serhii', 'Puchkovskyi'); echo gettype($name); // string ```