[TYPO3-core] RFC: #9318: Enhance and fix tslib_eidtools

Dmitry Dulepov [typo3] dmitry at typo3.org
Fri Sep 12 21:30:55 CEST 2008


Hi!

Ingo Renner wrote:
> What is this?
> 
> +    private static function getTSFE() {
> +        // Cached instance
> +        static $tsfe = null;
> 
> You might want to put the $tsfe into the class scope instead of the 
> method scope?! You can still have it private there if needed.

Yes I did this initially. Next I though it would be better to hide this variable even from other class methods. I use a kind of "factory" pattern to fetch TSFE. The reason for this is simple. If a private variable exists, there is a chance that it will be used in future without initializing. Just a simple programmer's mistake: variable exists - use it. By hiding the variable inside the method I ensure that it will be always correctly initialized.

For example, suppose there is a theoretical method added by a developer:

function bar() {
	self::initFeUser();
	self::$tsfe->whatever();
}

This will work because initFeUser() initializes TSFE. Now, the same developer could do this by mistake:


function foo() {
	self::connectDB();
	self::$tsfe->whatever();
}

The code is similar but TSFE is not initialized. With my approach such errors are not possible.

Does it make sense?

-- 
Dmitry Dulepov
TYPO3 Core team
My TYPO3 book: http://www.packtpub.com/typo3-extension-development/book
In the blog: http://typo3bloke.net/pages/book-reviews/presentation-zen-by-garr-reynolds/


More information about the TYPO3-team-core mailing list