I know I can achieve this with object Manager - but just wanted to see if there is a better way. Perhaps this case is a good candidate for using the object Manager; however, I would like to do things properly so it'd be good to get some feedback from more experienced developers.
I want to inject an object via a class constructor - and then within the body of the constructor decide what type of object it is.
I will use the Module Manager to check if a module exists - if so then I'd like to use one of its classes. Otherwise, I'd like to use another class in core.
The reason it is possible is because both the classes (the one that might not be available and the one in the core) have the same class name and method that I need to use).
Obviously I can't inject the class that might not exist directly. Here is some code as an example:
/**
* MyClass constructor.
*
* @param {unsure how to define it} $object
* @param \Magento\Framework\Module\Manager $moduleManager
*/
public function __construct({unsure how to define it} $object,
\Magento\Framework\Module\Manager $moduleManager,
)
{
$this->_moduleManager = $moduleManager;
if ($this->_moduleManager->isEnabled('ThirdPartyExtension_PerhapsNotInstalled')) {
$object = $classThatMightNotExist;
} else {
$object = $coreClass;
}
$this->_object = $object;
}