Req #66012 [NEW]: "initialize" keyword allowing member caching
| From: | llmll at gmx dot de | Date: | Thu, 31 Oct 2013 15:26:33 +0000 |
| Subject: | Req #66012 [NEW]: "initialize" keyword allowing member caching | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-182518@lists.php.net to get a copy of this message | ||
From: llmll at gmx dot de
Operating system: any
PHP version: Irrelevant
Package: Variables related
Bug Type: Feature/Change Request
Bug description:"initialize" keyword allowing member caching
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 bug report at https://bugs.php.net/bug.php?id=66012&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=66012&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=66012&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=66012&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=66012&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=66012&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=66012&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=66012&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=66012&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=66012&r=support
Expected behavior: https://bugs.php.net/fix.php?id=66012&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=66012&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=66012&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=66012&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=66012&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=66012&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=66012&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=66012&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=66012&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=66012&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=66012&r=mysqlcfg