php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #39280 Support for lazy evaluation type
Submitted: 2006-10-27 15:12 UTC Modified: 2012-09-23 16:53 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: arendjr at gmail dot com Assigned: nikic (profile)
Status: Closed Package: *General Issues
PHP Version: 5.1.6 OS: All
Private report: No CVE-ID: None
 [2006-10-27 15:12 UTC] arendjr at gmail dot com
Description:
------------
This is a feature request for a special language feature in PHP. I'm not sure how hard this feature would be to implement, but given the scripting nature of PHP I can imagine it not being too hard and it would be very useful if PHP6 could support this.

What I want to ask for is a new variable type that would support lazy evaluation. I'm not sure what would be a suitable syntax for such a feature, but let's assume for a moment it would use sqaure brackets.

This feature would allow someone to write code like:

$foo = [1+1];

This statement would basically store the string "1+1" in $foo, and $foo would be marked as being of type "lazy". Only  when at some point the value of $foo is being read, the code  will be evaluated and the real result will be stored in $foo. So, if someone would subsequently execute the following statement:

echo $foo;

This would actually expand to:

$foo = eval("return (1+1);");
echo $foo;

This feature would make it very easy to write code that would conditionally avoid potentially expensive statements.

As an example, imagine a case where you have to call some function bar() that takes an argument from which you do not know in advance whether the function will actually use the argument because whether the argument is used depends on some external factor. However, calculating the argument may be a very CPU intensive task. In this case you could write:

bar([someVeryExpensiveFunction()]);

Thus avoiding the execution of someVeryExpensiveFunction() in the case bar() does not actually use its value.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-09-23 16:53 UTC] nikic@php.net
Closing as closures are supported as of PHP 5.3 and they can be used to lazily evaluate code.
 [2012-09-23 16:53 UTC] nikic@php.net
-Status: Open +Status: Closed -Package: Feature/Change Request +Package: *General Issues -Assigned To: +Assigned To: nikic
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Jul 14 05:01:34 2025 UTC