Magento2: Argument 1 [...] must be an instance of Magento\Framework\App\Helper\Context

Magento2: Argument 1 [...] must be an instance of Magento\Framework\App\Helper\Context

September 23, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento2: Argument 1 [...] must be an instance of Magento\Framework\App\Helper\Context

Magento2: Solving Context Argument Errors Without Deleting DI Folders

Introduction

Welcome to my blog! I’m excited to share some insights into a common challenge faced by many developers new to Magento 2—argument errors related to the Magento\Framework\App\Helper\Context class. If you’re seeing this error, fear not; in this post, we’ll dive deep and find a solution that doesn’t involve deleting crucial folders like var/di.

Understanding the Context Argument Error

Let’s first understand what’s happening under the hood. When you see an error message that looks something like this:

Argument 1 passed to Cefar\AO\Helper\Ao::__construct() must be an instance of Magento\Framework\App\Helper\Context, instance of Magento\Framework\ObjectManager\ObjectManager given

It means that while your Ao.php file expects a Magento\Framework\App\Helper\Context object as its first argument, the ObjectManager is instead providing an instance of itself (Magento\Framework\ObjectManager\ObjectManager).

Why Does This Happen?

This error occurs because Magento’s ObjectManager is trying to resolve dependencies based on type hints in your constructor. In this case, it’s misinterpreting the context parameter.

The core issue lies in how you’re using inheritance from AbstractHelper. Let me explain:

class Ao extends \Magento\Framework\App\Helper\AbstractHelper {
    // Your code here
}

When you extend AbstractHelper, Magento expects your class to follow certain conventions. However, the error occurs because of a discrepancy between what your constructor declares and what AbstractHelper actually constructs.

The Deeper Issue

The problem isn’t with your constructor; it’s with how Magento resolves dependencies when you’re using inheritance. When you extend from an abstract helper, Magento doesn’t directly inject the context into your class—it uses a different method.

The Solution: Overriding the Context Injection

To resolve this issue without deleting DI folders, you need to override the context injection manually in your constructor. Update your Ao.php file as follows:

namespace Cefar\AO\Helper;

class Ao extends \Magento\Framework\App\Helper\AbstractHelper
{
    const DEFAULT_GRID_COLS = 4;

    protected $_session;
    protected $context; // Add this line

    public function __construct(
        \Magento\Framework\App\Helper\Context $context, // Change the order of arguments here
        \Magento\Customer\Model\Session $session
    )
    {
        parent::__construct(); // Remove the context argument from here
        $this->context = $context; // Inject it manually here
        $this->_session = $session;
    }

    // Rest of your methods remain unchanged...
}

By changing the order of arguments in the constructor and injecting the context manually, you’re explicitly overriding Magento’s default behavior. This tells the ObjectManager exactly what dependencies to resolve.

Why Does This Work?

This solution works because it bypasses Magento’s automated dependency resolution for abstract classes. By manually injecting the context, we ensure that the correct object is passed to our constructor.

Conclusion

In this post, we explored a common challenge faced by developers new to Magento 2—the context argument error when extending AbstractHelper. We discovered the root cause of the issue and provided a robust solution that doesn’t involve deleting crucial DI folders.

Remember, when working with inheritance in Magento, it’s important to understand how dependencies are resolved. In most cases, you don’t need to worry about this; but for abstract helpers like this one, manual injection is the way forward.

I hope this post has been insightful and will help you navigate the intricacies of dependency injection in Magento 2. For more advanced topics on PHP development, feel free to explore our blog at LaravelCompany. Happy coding!

Magento 2 Errors

Our network