[TYPO3-dev] Re: Best-practice: dependencies in Controllers
Philipp
philippwrann at gmx.at
Wed Feb 12 14:20:03 CET 2014
If in extbase context:
get instance of objectmanager with
$objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('\TYPO3\CMS\Extbase\Object\ObjectManager');
then load your singleton classes with
$objectManager->get('Vendor\\Extension\\Path\\To\\Class');
to instanciate new prototypes use
$objectManager->create('Vendor\\Extension\\Path\\To\\Model',$construcotArgument1,$constructorArgument2);
If you want an instance of a very simple class with no dependencies, etc simply use
$object = new \Vendor\Package\Path\To\Model($arg1,$arg2);
if you are in ControllerContext or a Repository you allready have an injected ObjectManager
$this->objectManager->get()...
A singleton class is a class that must not exist multiple, only one instance is allowed. You need that for example when using a sessioncache or something within the class. Therefore a singleton class must not have a contructor. To make clear that a variable represents a singleton you use objectManager->get and not objectManager->create
When asking the objectManager for a singleton class it allways returns you the same instance.
You can define a singleton class by implementing the singleton interface \TYPO3\CMS\Core\SingletonInterface
When you need some coreclass not related to extbase you can call it directly using makeInstance.
More information about the TYPO3-dev
mailing list