# Magento 2 - How to Get Configuration Value Programmatically?

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">I wrote this article under Magento 2 version of 2.4.6, latest version might have difference approach.</div>
</div>

There are several ways to retrieve system configuration, but there are 2 common approaches, they are using <s>object manager</s> and by injecting into construct method *(\_\_construct is the recommended one by Magento itself).*

### 1\. by Object Manager

```php
.....
$scopeConfig = \Magento\Framework\App\ObjectManager::getInstance()
    ->get(\Magento\Framework\App\Config\ScopeConfigInterface::class);

$configValue = $scopeConfig->getValue(
        'fiko/general/testing_config',
        \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
    );
.....
```

Notes:

* <mark>$scopeConfig = \Magento\Framewo....</mark>: we need to define what's the instance / class we want to import / use.
    
* <mark>$configValue = $scopeCon....</mark>: time to retrieve the configuration, further notes will be on 2nd solution below
    

### 2\. by Injecting into construct method **(Recommended)**

```php
<?php

namespace Fiko\Training\Helper;

class Data
{
    public $scopeConfig;

    public function __construct(
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
    ) {
        $this->scopeConfig = $scopeConfig;
    }
}
```

Notes:

* <mark>public $scopeConfig;</mark>: by the time PHP 8 released, it is mandatory to define the visibility *(no dynamic property allowed)*. it can be public/protected/private, but in this case I would use public as it can be used by other instances.
    
* <mark>ScopeConfigInterface $scopeConfig</mark>: interface we need to have in order to retrieve configuration.
    

```php
public function getExampleConfig()
{
    $configValue = $this->scopeConfig->getValue(
        'fiko/general/testing_config',
        \Magento\Store\Model\ScopeInterface::SCOPE_STORE
    );

    return $configValue;
}
```

Notes:

* <mark>fiko/general/testing_config</mark>: it is the path of the configuration.
    
* <mark>\Magento\Store\Model\ScopeInterface::SCOPE_STORE</mark>: It is the scope area (I assume you already know this).
    
    * I recommend to use SCOPE\_STORE, as it will automatically retrieve the higher scope level (website or default) if it doesn't exist on store level.
        

---

References:

* [https://www.magetrend.com/blog/magento-2-get-config-value/](https://s.id/1UhIy)
    
* [https://magento.stackexchange.com/a/87835/106128](https://s.id/1UhIA)
