1

With the code below, I'm trying to instantiate an helper class from my custom module.

// Sanitize the $_GET[] value
// Create an object of the order loaded with the $order_id passed as query string.
$orderId = htmlspecialchars($_GET["order_id"]);
$order = $objectManager->create('Magento\Sales\Model\Order')->loadByIncrementID($orderId);

// Instantiate app/code/Perfectmakeupmirrors/Order/Helper/Data.php // call assemble_order_info(Order $order) $helper = $objectManager->get('\app\code\Perfectmakeupmirrors\Order\Helper\Data');

Please guide me on the right way to do it.

CodeForGood
  • 764
  • 7
  • 31
  • Hi there it is not allowed to use the object manager as the code standard of PHPMD, PHPCS suggests try using the dependency injector for instantiating the class objects.You can see more about the dependency injection in this link = https://developer.adobe.com/commerce/php/development/components/dependency-injection/, and about your question can you please explain this part again as i did not understood it correctly = in the directory at the same level as the root in magento ? – Bharath Kumar Oct 08 '22 at 17:07
  • Hi, I meant both testcode directory and Vendor/Magento are in public_html directory. – CodeForGood Oct 09 '22 at 05:13

1 Answers1

1

To retrieve a helper class from your custom module, replace your code:

$helper = $objectManager->get('\app\code\Perfectmakeupmirrors\Order\Helper\Data');

to

$helper = $objectManager->get('Perfectmakeupmirrors\Order\Helper\Data');

Btw, the Original Poster would like to use his code for testing purposes, so we can use ObjectManager. We should not (almost never) use ObjectManager in real code, there is a post discussing this To use or not to use the ObjectManager directly?

Tu Van
  • 6,868
  • 2
  • 11
  • 22