<p>Rather than assuming that the providers will live in a specific location, I'd be inclined to implement a <a href="https://craftcms.com/docs/plugins/hooks-and-events#creating-hooks" rel="nofollow noreferrer">custom hook</a>, which allows third party plugins to register a new provider.</p>

<p>This also allows third parties to do any initialisation of their provider, prior to handing it to you.</p>

<p>For example, your plugin could do something like this:</p>

<pre><code>$providers = craft()-&gt;plugins-&gt;call('registerProviders');
</code></pre>

<p>And a third party could then do something like this, in its main plugin file:</p>

<pre><code>public function registerProviders()
{
    $instance = new MyProvider('custom', 'dependencies');

    return ['myProvider' =&gt; $instance];
}
</code></pre>

<p>Greatly simplified, but hopefully you get the idea.</p>

CC BY-SA 3.0
2017-03-17T08:35:26.027Z
2017-03-17T08:35:26.027Z
Stephen Lewis
<p>I'm in the process of building a plugin that'll have extensible 'Providers' to allow developers to build connections to other services. These 'Providers' will just be classes that are built to an interface I've included in the plugin. (It's currently in an 'interfaces' directory of my plugin; I'm not sure about the best-practice for where interfaces should be stored.)</p>

<p><strong>My question:</strong> What I'm having trouble with is how to include the developer's Provider class when I don't know what the name will be. Here's what I'm doing now:</p>

<pre><code>require_once(CRAFT_PLUGINS_PATH . 'location/providers/' . $this-&gt;settings-&gt;provider . 'Provider.php');

$provider = '\Craft\\' . $this-&gt;settings-&gt;provider . 'Provider';

return new $provider();
</code></pre>

<p><code>$this-&gt;settings-&gt;provider</code> just returns the 'handle' of their Provider, like <code>sampleService</code>. The file they place in the <code>providers</code> directory would be <code>sampleServiceProvider.php</code>, in that case.</p>

<p>If something doesn't make sense, I'll gladly clarify. Any help is <em>greatly</em> appreciated!</p>

<plugin-development><plugins><services>
Loading dynamically-named classes in a plugin's service
# Loading dynamically-named classes in a plugin's service

(Asked by: username_0 on 2017-03-16)

I'm in the process of building a plugin that'll have extensible 'Providers' to allow developers to build connections to other services. These 'Providers' will just be classes that are built to an interface I've included in the plugin. (It's currently in an 'interfaces' directory of my plugin; I'm not sure about the best-practice for where interfaces should be stored.)
My question: What I'm having trouble with is how to include the developer's Provider class when I don't know what the name will be. Here's what I'm doing now:
require_once(CRAFT_PLUGINS_PATH . 'location/providers/' . $this->settings->provider . 'Provider.php');

$provider = '\Craft\\' . $this->settings->provider . 'Provider';

return new $provider();

$this->settings->provider just returns the 'handle' of their Provider, like sampleService. The file they place in the providers directory would be sampleServiceProvider.php, in that case.
If something doesn't make sense, I'll gladly clarify. Any help is greatly appreciated!

**Tags:** plugin-development, plugins, services


---

## 1 answers

### Answer by username_1 on 2017-03-17. Score: 1

Rather than assuming that the providers will live in a specific location, I'd be inclined to implement a custom hook, which allows third party plugins to register a new provider.
This also allows third parties to do any initialisation of their provider, prior to handing it to you.
For example, your plugin could do something like this:
$providers = craft()->plugins->call('registerProviders');

And a third party could then do something like this, in its main plugin file:
public function registerProviders()
{
    $instance = new MyProvider('custom', 'dependencies');

    return ['myProvider' => $instance];
}

Greatly simplified, but hopefully you get the idea.


---

