1

Having one module named Override\Search in which two helper classes are there.

1. Override\Search\Helper\Data.php 
2. Override\Search\Helper\Util.php

Both helper are depends on each other, thus giving circular dependency error.

Exception #0 (LogicException): Circular dependency: 

Try 1: - using their namespaces in header as follows but still facing same error:

use \Override\Search\Helper\Data as Override_MainHelper;

Try 2: - Adding initializing in constructor as below

public function __construct(
        //Other classes
        \Override\Search\Helper\Data $searchHelperMain
    ) {
        //Other classes
        $this->_searchHelperMain = $searchHelperMain;
    }

Following class is Main Data.php

<?php
namespace Override\Search\Helper;
use \Magento\Store\Model\StoreManagerInterface;
use \Magento\Backend\Model\Url;
use \Override\Search\Helper\Util;
use \Psr\Log\LoggerInterface;
use \Magento\Catalog\Model\Product;
use Magento\Config\Model\ResourceModel\Config\Data\Collection;
use \Magento\Framework\App\RequestInterface;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $_storeModelStoreManagerInterface;
    /**
     * @var \Magento\Backend\Model\Url
     */
    protected $_backendModelUrl;
    /**
     * @var \Override\Search\Helper\Util
     */
    protected $_searchHelperConfig;
    /**
     * @var \Psr\Log\LoggerInterface
     */
    protected $_psrLogLoggerInterface;
    /**
     * @var \Magento\Catalog\Model\Product
     */
    protected $_catalogModelProduct;
    /**
     * @var \Magento\Tax\Helper\Data
     */
    protected $_taxHelperData;

    /**
     * @var \Magento\Eav\Model\Entity\Type
     */
    protected $_modelEntityType;

    /**
     * @var \Magento\Eav\Model\Entity\Attribute
     */
    protected $_modelEntityAttribute;

    /**
     * @var \Magento\Framework\Locale\CurrencyInterface
     */
    protected $_localeCurrency;

    /**
     * @var Magento\Directory\Model\CurrencyFactory
     */
    protected $_currencyFactory;

    /**
     * @var Magento\Config\Model\ResourceModel\Config\Data\Collection
     */
    protected $_configDataCollection;
    /**
     * @var \Magento\Store\Model\Store
     */
    protected $_frameworkModelStore;
    protected $_data_features_response;
    protected $_data_enabled_feature_response;
    public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeModelStoreManagerInterface,
        \Magento\Backend\Model\Url $backendModelUrl,
        \Override\Search\Helper\Util $searchHelperConfig,
        \Psr\Log\LoggerInterface $psrLogLoggerInterface,
        \Magento\Catalog\Model\Product $catalogModelProduct,
        \Magento\Catalog\Helper\Data $taxHelperData,
        \Magento\Eav\Model\Entity\Type $modelEntityType,
        \Magento\Eav\Model\Entity\Attribute $modelEntityAttribute,
        \Magento\Directory\Model\CurrencyFactory $currencyFactory,
        \Magento\Framework\Locale\CurrencyInterface $localeCurrency,
        \Magento\Config\Model\ResourceModel\Config\Data\Collection $configDataCollection
    ) {

        $this->_storeModelStoreManagerInterface = $storeModelStoreManagerInterface;
        $this->_backendModelUrl = $backendModelUrl;
        $this->_searchHelperConfig = $searchHelperConfig;
        $this->_psrLogLoggerInterface = $psrLogLoggerInterface;
        $this->_catalogModelProduct = $catalogModelProduct;
        $this->_taxHelperData = $taxHelperData;
        $this->_modelEntityType = $modelEntityType;
        $this->_modelEntityAttribute = $modelEntityAttribute;
        $this->_localeCurrency = $localeCurrency;
        $this->_currencyFactory = $currencyFactory;
        $this->_configDataCollection = $configDataCollection;
    }

   //Other Functions

}

Following class is Main Util.php

<?php
namespace Override\Search\Helper;

use \Magento\Framework\App\Config\ScopeConfigInterface;
use \Magento\Framework\UrlInterface;
use \Magento\Store\Model\StoreManagerInterface;
use \Override\Search\Model\Product\Sync;
use \Magento\Framework\Model\Store;
use \Override\Search\Model\Api\Action\Features;
use \Override\Search\Helper\Data;
//use \Override\Search\Helper\Data as Override_MainHelper;

class Util extends \Magento\Framework\App\Helper\AbstractHelper
{
    /**
     * @var \Override\Search\Helper\Data
     */
    protected $_searchHelperMain;

    public function __construct(
        //Other classes
        \Override\Search\Helper\Data $searchHelperMain
    ) {
        //Other classes
        $this->_searchHelperMain = $searchHelperMain;
    }

    //Other Functions

}

Having some idea about proxy and factories class but not sure what are pros and cons of those features except some lazy loading stuff and dependency injection.

http://alanstorm.com/magento_2_object_manager_proxy_objects

Circular dependency: Magento\Backend\Helper\Data depends on Magento\Backend\Model\Url and vice versa

https://devdocs.magento.com/guides/v2.3/extension-dev-guide/proxies.html

What is suitable here that can cover backward compatibility as well i.e. 2.1.x and 2.2.x and 2.3.x? Proxy or Factories or any 3rd way?

2
  • 1
    I faced a similar problem. Not really a solution but in the end I had a rethink and structured it so that several helpers all called the same single 'abstract' helper and not each other. I use the term abstract to demonstrate point. Wasnt actual abstract class. Basically I had to refactor a load of stuff. I'm still not sure the proper way to use a proxy. Your not supposed to inject them. This is best explanation I can find magento.stackexchange.com/a/143174/70343 which in my opinion makes them redundant Commented Jun 30, 2019 at 20:16
  • @DominicXigen i read that too. thanks for suggestion to make abstract helper i guess, that may work rather to go into this DI
    – Softec
    Commented Jul 2, 2019 at 15:44

0

Your Answer

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