php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #72637 FR: Lazy statement operator
Submitted: 2016-07-20 23:29 UTC Modified: 2016-07-21 02:33 UTC
From: david dot proweb at gmail dot com Assigned:
Status: Suspended Package: Scripting Engine problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2016-07-20 23:29 UTC] david dot proweb at gmail dot com
Description:
------------
Should be great if PHP could have a lazy statement operator. 

Imagines that a lazy statement operator is like an anonymous function that could be called as a variable or property (it basically translate this function into the variable value at runtime when it is readed or writed the first time). It is very useful because you don't need to test all the time what is the type of this variable is before you run that.


    Simulation simulation using current PHP:

        // Defining the lazy property.
        $someClass->someProperty = function () {
            return 1;
        };

        // In some moment, ONLY when I read this property:
        // It basically will "compile" the statement and set to own property or variable.
        if ($someClass->someProperty instanceof LazyStatement) {
            $someClass->someProperty = $someClass->someProperty();
        }

        // Now I print it normally.
        echo $someClass->someProperty.


    Implementation by using the lazy operator:

        $someClass->someProperty = lazy { return 1; }
        echo $someClass->someProperty;


It could be used with some codes where you don't need use it all the time, only for specific cases that depends basically of user demand. Imagine that a framework, for instance, could define some lazy properties that are only calculated when user request it, and not all the time or by using magic methods.


    Simulation simulation using current PHP:

        /**
         * @property-read int $value
         */
        class Test {
            // Note: should be declared as private.
            // And should be declared as a property on doc.
            private $value;

            public function __get($key) {
                if ($key === 'value') {
                     if ($this->value === null) {
                         $this->value = calculatePrimeNumberNth(10000);
                     }

                     return $this->value;
                }
            }
        }

        $test = new Test;
        echo $test->value;

    
    Implementation by using the lazy operator:

        class Test {
            public $value = lazy { return calculatePrimeNumberNth(10000); }
        }

        $test = new Test;
        echo $test->value;


It should works to be writed too. Basically, on write the lazy implementation should be executed and setted on object-variable before to run the writer code, so things like that are valids:


    $array = lazy { return [ 1, 2, 3 ]; }
    $array[] = 4;
    
    $array now is [ 1, 2, 3, 4 ]


And should be possible works with use() like anonymous functions, it should be useful to use local scoped informations.


    $number = 123;
    $text   = lazy use ($number) { return (string) $number; }

    $number = 456;

    echo $text;    

    Should prints 456, instead of 123 that was overwroted.


It should allow complexes statements too, but should return something (else, will be returned null, what make no sense):


    $number = lazy {
            $sum = random_int(0, 100);

            return $sum;
        };

    echo $number;


It could PHP IDEs to check some conditions where variables are readed optionally and tells to use lazy operator, or then check conditions where lazy operator are not need:


    Where could use lazy operator:

        $number = calculatePrimeNumberNth(10000);

        if (complexTest()) {
            return $number;
        }

        if (anotherComplexTest()) {
            doSomethingBefore();

            return $number;
        }

        return 0;

        IDE Warning: you could use lazy operator on line 0 because $number could be not used in all the time.


    Where is not useful to use lazy operator:

        $number = lazy { return 123; }
        echo $number;

        IDE Warning: lazy operator at line 0 could be removed because $number is always used on code.


    Simulation simulation using current PHP:
    
        $number = function () { return calculatePrimeNumberNth(10000); };
        // Same code after.

    
    Implementation by using the lazy operator:
    
        $number = lazy { return calculatePrimeNumberNth(10000); };
        // Same code after.




Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-07-21 02:33 UTC] requinix@php.net
-Status: Open +Status: Suspended -Package: Performance problem +Package: Scripting Engine problem
 [2016-07-21 02:33 UTC] requinix@php.net
This is the kind of feature request that needs a discussion on the mailing list and an RFC. https://wiki.php.net/rfc/howto
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 21:01:30 2024 UTC