|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2013-10-31 16:59 UTC] johannes@php.net
-Status: Open
+Status: Wont fix
[2013-10-31 16:59 UTC] johannes@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 08:00:01 2025 UTC |
Description: ------------ PHP should provide a method to initialize (static or instance) members of objects and later present cached data. Motivation is to hide the program logic for first-time fetching of data, which is important for ORM classes. $Property gets a new keyword "initialize" and a block, which is executed if and only if $Property === null. The called blocks returns a value, that is onwards stored in this property. Subsequent calls just return the value. So the return value is cached! The cached value can later be cleared, just by setting $Property = null; This keyword also candidates for static property initializers! Thanks for reading and thinking. Test script: --------------- class Foo { public $BigData; public function InitializeBigData() { // this has to be called somehwere // and tends to get forgotten // or calle to late, after some code // accesses $BigData directly and hits null $this->BigData = somehow::getBigData(); } public function GetBigData() { // smarter method, but must be called every time // when you want to access BigData // but is not a property // too much writing overhead for many properties if ($this->BigData === null) { $this->BigData = somehow::getBigData(); } return $this->BigData; } } Expected result: ---------------- class Foo { public $BigData initialize { // this block gets executed only, if property is null // fetches big data and stores it internally somewhere return somehow::getBigData(); } } $foo = new Foo(); // don't care about BigData being read or cached, its just there. echo $foo->BigData; // reset cached value $foo->BigData = null; // will load BigData again echo $foo->BigData;