Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:94617 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 93442 invoked from network); 21 Jul 2016 15:52:01 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 21 Jul 2016 15:52:01 -0000 Authentication-Results: pb1.pair.com header.from=david.proweb@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=david.proweb@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.216.178 as permitted sender) X-PHP-List-Original-Sender: david.proweb@gmail.com X-Host-Fingerprint: 209.85.216.178 mail-qt0-f178.google.com Received: from [209.85.216.178] ([209.85.216.178:35766] helo=mail-qt0-f178.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 6C/34-52781-0AFE0975 for ; Thu, 21 Jul 2016 11:52:01 -0400 Received: by mail-qt0-f178.google.com with SMTP id x25so46346261qtx.2 for ; Thu, 21 Jul 2016 08:52:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to; bh=YcO7FSRMQou/nEOEDQiZw0FyprLf8GwtEbbkJ5vVxCo=; b=oRPd6alMAM3D3qaxdh+tTgZIDnp1ZYagBQsJEQ/IEGrNigxIXqc5Xhcdvis2nVf5+r eegVYux4tknobh6znHMXN1lU2qCH0umE2h7jqbBhboj5eYYBGwU9pI20oyG5yMwQonqr zAmXkpUnpPJs12j/Bta2Wxh6aQcaymvSCgVfEt0nLtuL6YOsp8Tnhczl8jcfj99xLTxI m/cTepB9dz36DcHIWvA+hKRjWXanTVpuaTxuwqIejwPeGOnPvEGOv4cDE1BD/vo2HA4I DjAhxPgce0o+hNeV9XdM4jbSoHruEYHG26fC9VWxIEeubQcEWC2OtSjBsIpknKhguy74 EPjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=YcO7FSRMQou/nEOEDQiZw0FyprLf8GwtEbbkJ5vVxCo=; b=Z6FWSbRsOFShkR/f6IfnSOPcB55bgrpq3EtIR9Oa3aQ021rhyP/YcwLQJVXvvQefdz oiaY1qFAEHbQEQ03N0nCgRD5xG1+xuUOxCRJole9lzleIY+JS5CsU/PzI1BEjkXFt17Q jXqI2on5/6A3O/kVw58miNjk44+nerWqF/CjcQyml7egJPuuQFlTXZZ+XDAYWZM55VWm KcnDR1zG+ymo0XWRfHAHhR5wGwDo8eCN18eeCvFSvVLUCIFsq7ebB/06EU3exAae37Sp sdTb/e90dUloG1N4IZNh/cb3X0s0YGMDQZKLlj/STdoSJb9fyd06o14K+RXGerG2O60w arog== X-Gm-Message-State: ALyK8tIvQiBdmKt3RIaQ9B23dytcoAlrCLzzN/lbvn/vBs28Bphdk4vi6KLRylBOT2gLqIx7fO5n4ifoimKLRw== X-Received: by 10.200.48.193 with SMTP id w1mr72664688qta.8.1469116318054; Thu, 21 Jul 2016 08:51:58 -0700 (PDT) MIME-Version: 1.0 Received: by 10.55.45.67 with HTTP; Thu, 21 Jul 2016 08:51:38 -0700 (PDT) Date: Thu, 21 Jul 2016 12:51:38 -0300 Message-ID: To: PHP Internals Content-Type: text/plain; charset=UTF-8 Subject: RFC: lazy statements From: david.proweb@gmail.com (David Rodrigues) This Feature Request is about the implementation of lazy statements. Basically it should implements a 'lazy' keyword (or similar) and structured like an anonymous function, except that it doesn't accepts arguments, but accept the use() feature. $var = lazy { return 1; } $var = lazy use ($x) { return $x; } It differ from anonymous function because this assignment is treated as variable, without need to call or check if it is a valid Closure all the time. --- Execution example: echo $var; echo $var; In this example, the first echo will execute the lazy statement and set the returned value directly to $var container, then will echo 1. The second echo will not execute the lazy anymore, because it was processed on first echo, so will just echo 1. --- It should be very useful on framework or package development because you can defines lazy properties that need to be calculated only when it is requested the first time. Should be useful too when you have a global variable like $user that you should not use all the time (in all requests). View::share('user', lazy { return User::find(session('user.id')); }); This code will be executed only on try read or write on $user variable. --- It should works from read or write, to the next code is valid: $var = lazy { return [ 1, 2, 3 ]; }; $var[] = 4; --- Currently it is possible do it in a very hackish way: by using magic getter, by create a user getter or a own implementation of this feature. All this ways have a lot of limitations. A hackish example that shows how it should works on current PHP version: http://pastebin.com/cz93wL7n --- I tell more about that here: https://bugs.php.net/bug.php?id=72637 -- David Rodrigues