Req #66012 [Opn->Wfx]: "initialize" keyword allowing member caching
| From: | johannes@php.net | Date: | Thu, 31 Oct 2013 16:59:09 +0000 |
| Subject: | Req #66012 [Opn->Wfx]: "initialize" keyword allowing member caching | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-182529@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=66012&edit=1
ID: 66012
Updated by: johannes@php.net
Reported by: llmll at gmx dot de
Summary: "initialize" keyword allowing member caching
-Status: Open
+Status: Wont fix
Type: Feature/Change Request
Package: Variables related
Operating System: any
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
You can easily do this in a constructor, actually this is what a constructor is for. Extneding the
language for this makes the language more complex, causes issues with execution order, error
handling and probably other things which you now can handle.
Previous Comments:
------------------------------------------------------------------------
[2013-10-31 15:26:32] llmll at gmx dot de
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;
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=66012&edit=1