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.

2
  • 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 = developer.adobe.com/commerce/php/development/components/…, 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 ? Commented Oct 8, 2022 at 17:07
  • Hi, I meant both testcode directory and Vendor/Magento are in public_html directory. Commented Oct 9, 2022 at 5:13

1 Answer 1

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?

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.