use Magento\Framework\ObjectManagerInterface; class MyCustomClass { /** * @var ObjectManagerInterface */ protected $objectManager; /** * MyCustomClass constructor. * @param ObjectManagerInterface $objectManager */ public function __construct(ObjectManagerInterface $objectManager) { $this->objectManager = $objectManager; } /** * Get an instance of an object */ public function getObjectInstance() { return $this->objectManager->create('Magento\Catalog\Model\Product'); } } // Usage $myCustom = new MyCustomClass($objectManager); $myCustom->getObjectInstance();
use Magento\Framework\ObjectManagerInterface; class MyCustomClass { /** * @var ObjectManagerInterface */ protected $objectManager; /** * MyCustomClass constructor. * @param ObjectManagerInterface $objectManager */ public function __construct(ObjectManagerInterface $objectManager) { $this->objectManager = $objectManager; } /** * Configure an object */ public function configureObject() { $product = $this->objectManager->create('Magento\Catalog\Model\Product'); $product->setName('Test Product'); $product->setPrice(50); $product->setDescription('This is a test product.'); $product->save(); } } // Usage $myCustom = new MyCustomClass($objectManager); $myCustom->configureObject();Magento\Framework package library is being used in the code examples.