1

I tried to call the class using construct method.

    <?php
namespace Vendor\Module\Helper;

class Data extends AbstractHelper

{

/**
 * @var \Magento\Framework\App\Config\ScopeConfigInterface
 * */
protected $scopeConfig;

/**
 * @var \Magento\Framework\App\Filesystem\DirectoryList
 * */
protected $_directorylist;
/**
 * @var \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress
 * */
protected $remoteAddress;
/**
 * constructor
 * \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
 * \Magento\Framework\Data\Form\FormKey $formKey
 * \Magento\Framework\App\Filesystem\DirectoryList $_directorylist
 * */
public function __construct(
    \Magento\Framework\App\Helper\Context $context,
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
    \Magento\Framework\App\Filesystem\DirectoryList $directorylist,
    \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $remoteAddress
) 
{
    parent::__construct($context);
    $this->_directorylist = $directorylist;
    $this->scopeConfig = $scopeConfig;
    $this->remoteAddress = $remoteAddress;
}

/**
 * @param $config_path 
 * @return config_value
 * */
public function getConfig($config_path)
{
    return $this->scopeConfig->getValue(
        $config_path,
        \Magento\Store\Model\ScopeInterface::SCOPE_STORE
    );
}

/**
 *  
 * @return media_path
 * */
public function getMediaPath()
{
    return $this->_directorylist->getPath('media');
}

/**
 *  
 * @return string
 * */
public static function getMagentoFormKey() 
{
    // @codingStandardsIgnoreStart 
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    return $objectManager->get('Magento\Framework\Data\Form\FormKey')->getFormKey();
    // @codingStandardsIgnoreEnd
}

//function to get the current user ip
public static function get_user_ip() 
{
    return $this->remoteAddress->getRemoteAddress();
    // @codingStandardsIgnoreStart 
    //$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    //return $objectManager->get('Magento\Framework\HTTP\PhpEnvironment\RemoteAddress')->getRemoteAddress();
    // @codingStandardsIgnoreEnd
}

}

I got this error Uncaught Error: Using $this when not in object context in /Applications/MAMP/htdocs/magento/app/code/Vendor/Module/Helper/Data.php:95

Can anyone please help to me for this?

Thanks

  • Your code is not even 95 line of code, please make sure you added the whole code to your question. – Mohit Rane Aug 2 at 5:02
  • agree with @Dominic Xigen remove the static keyword in the function – Mohit Rane Aug 2 at 5:04
1

Remove static parts from public static

That's about it. Everything else looks good.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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