Skip to content

Instantly share code, notes, and snippets.

@a-r-m-i-n
Created March 29, 2021 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a-r-m-i-n/ab780623e7f7b262ac2f92e24ced9524 to your computer and use it in GitHub Desktop.
Save a-r-m-i-n/ab780623e7f7b262ac2f92e24ced9524 to your computer and use it in GitHub Desktop.
CreateInstanceTrait for TYPO3 CMS
<?php
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\StringUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
trait CreateInstanceTrait
{
/** @return parent */
public static function createInstance()
{
if (StringUtility::endsWith(static::class, 'Repository')) {
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
return $objectManager->get(static::class);
}
return GeneralUtility::makeInstance(static::class);
}
}
<?php
class MyAwesomeRepository extends Repository
{
use CreateInstanceTrait;
}
$repo = MyAwesomeRepository::createInstance();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment