$this->content !== NULL ???

$this->content !== NULL ???

by Howard Miller -
Number of replies: 6
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
This bit of code appears (uncommented... Grrr) at the start of the get_content() function of every/most blocks....



if($this->content !== NULL) {
return $this->content;
}


What, exactly, does it do? It looks like a caching thing but that leads me to wonder what the conditions for content being null (and, hence, the rest of the function being run).

The parent class has a method called refresh_content() which sets the content to null and calls get_content() but I don't see that this gets called from anyway... which seems strange.

Cheers smile
Average of ratings: -
In reply to Howard Miller

Re: $this->content !== NULL ???

by Hubert Chathi -
Yes, it is for caching. See http://docs.moodle.org/en/Development:Blocks#I_Just_Hear_Static

$this->content gets set to NULL initially, because the base class has "var $content = NULL;" (blocks/moodleblock.class.php:54)
In reply to Hubert Chathi

Re: $this->content !== NULL ???

by Deleted user -
Could teh function calling get_content not check if there is a cached version available? This is a much more clean solution.

Ries
In reply to Deleted user

Re: $this->content !== NULL ???

by Hubert Chathi -
It depends on what you mean by "clean". Object-oriented principles suggest that the caller should not need to know anything about the internal workings of get_content. It should just be able to call the function without worrying about how it produces the result.
In reply to Hubert Chathi

Re: $this->content !== NULL ???

by sam marshall -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers
Yes, but they also suggest that the various different implementations shouldn't all have to duplicate that code. A better approach would be:

public BlockContent getContent()
{
if(content == null)
{
content = innerGetContent();
}
return content;
}

protected abstract BlockContent innerGetContent();


Then you just override the 'inner' one in each block and you don't need to duplicate the cache logic. Caller uses outer getContent.

However, lazy-initialising is a good principle (definitely better than creating the content, which is usually the heaviest thing the block does, in constructor - what if it needs to refer to another block object or something...), so I don't think the current design is too bad really.

--sam

In reply to Hubert Chathi

Re: $this->content !== NULL ???

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Thanks.... I did read that documentation but it didn't make much sense to me. Having looked at the code again I see that get_content() can get called multiple times during the 'lifetime' of a block (e.g. a simple boolean check for the block having content at all) and this is merely to cache between those calls.