Skip to content

Instantly share code, notes, and snippets.

@alan-ps
Last active August 3, 2019 13:44
Show Gist options
  • Save alan-ps/b08b719b698a1f98ba97d7fa3a1ef83d to your computer and use it in GitHub Desktop.
Save alan-ps/b08b719b698a1f98ba97d7fa3a1ef83d to your computer and use it in GitHub Desktop.

Revisions

  1. alan-ps revised this gist Aug 3, 2019. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions php_interested_func_things.md
    Original file line number Diff line number Diff line change
    @@ -67,9 +67,6 @@ $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
    @@ -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)
  2. alan-ps revised this gist Aug 3, 2019. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions php_interested_func_things.md
    Original 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
  3. alan-ps revised this gist May 30, 2019. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions php_interested_func_things.md
    Original 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!';
    })();
    ```
  4. alan-ps revised this gist May 30, 2019. 1 changed file with 26 additions and 0 deletions.
    26 changes: 26 additions & 0 deletions php_interested_func_things.md
    Original 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
    ```
  5. alan-ps revised this gist May 29, 2019. 1 changed file with 27 additions and 1 deletion.
    28 changes: 27 additions & 1 deletion php_interested_func_things.md
    Original 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!
    ```
  6. alan-ps revised this gist May 29, 2019. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion php_interested_func_things.md
    Original file line number Diff line number Diff line change
    @@ -48,7 +48,7 @@ function myFunc(): void {
    sayWorld(); // Hello World!
    ```

    ### Lambda
    ### 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!
    ```
  7. alan-ps revised this gist May 29, 2019. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions php_interested_func_things.md
    Original 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
    ```
  8. alan-ps renamed this gist May 29, 2019. 1 changed file with 14 additions and 1 deletion.
    15 changes: 14 additions & 1 deletion php_interested_things.md → php_interested_func_things.md
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    <?php

    function myFunc(?MyObject $myObj) {
    echo 'Hello world!';
    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!
    ```
  9. alan-ps created this gist May 29, 2019.
    36 changes: 36 additions & 0 deletions php_interested_things.md
    Original 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
    ```