Bug #60174
closed
Singleton instance w/ GeneralUtility::makeInstance
Added by Jan Runte over 10 years ago.
Updated about 6 years ago.
Description
You cannot always get a "unique" singleton class with "GeneralUtility::makeInstance".
The static method doesn't check for an existing backslash at the beginning of "className".
use TYPO3\CMS\Core\Utility\GeneralUtility;
$singleton1 = GeneralUtility::makeInstance('TYPO3\CMS\Core\Page\PageRenderer');
$singleton2 = GeneralUtility::makeInstance('\TYPO3\CMS\Core\Page\PageRenderer');
$hash1 = spl_object_hash($singleton1);
$hash2 = spl_object_hash($singleton2);
var_dump( ($hash1 === $hash2) );
The result is FALSE. This shouldn't happen!
- Target version changed from 7.0 to next-patchlevel
Jan Runte wrote:
$singleton2 = GeneralUtility::makeInstance('\TYPO3\CMS\Core\Page\PageRenderer');
First of all, this code is broken.
But I agree that this can happen during development and errors like that are horrible to track.
For master I would suggest to throw an exception in this case to notify the developers about their broken code.
For 6.2 I suggest to silently fix this case by adding $className = ltrim($className, '\\') as we should not break code that already works on a released branch
We must do the same for the Extbase ObjectManager btw. as it also keeps track of singletons...
- Status changed from New to Under Review
Patch set 1 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at http://review.typo3.org/35751
Patch set 2 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at http://review.typo3.org/35751
Patch set 3 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at http://review.typo3.org/35751
Patch set 4 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at http://review.typo3.org/35751
Patch set 1 for branch TYPO3_6-2 of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at http://review.typo3.org/35836
- Status changed from Under Review to Resolved
- Status changed from Resolved to Closed
Also available in: Atom
PDF