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 Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: llmll at gmx dot de
New email:
PHP Version: OS:

 

 [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

Pull Requests

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: Sun Dec 22 06:01:30 2024 UTC