php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #66012 "initialize" keyword allowing member caching
Submitted: 2013-10-31 15:26 UTC Modified: 2013-10-31 16:59 UTC
From: llmll at gmx dot de Assigned:
Status: Wont fix Package: Variables related
PHP Version: Irrelevant OS: any
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
44 + 1 = ?
Subscribe to this entry?

 
 [2013-10-31 15:26 UTC] 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;


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-10-31 16:59 UTC] johannes@php.net
-Status: Open +Status: Wont fix
 [2013-10-31 16:59 UTC] johannes@php.net
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 00:01:28 2024 UTC