|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2012-09-23 16:53 UTC] nikic@php.net
[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
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Dec 06 06:00:01 2025 UTC |
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.