<p>In your example, if you do this:</p>

<pre><code>$var = new \XXX();
$var-&gt;doThis = 'CONTENT HERE';
</code></pre>

<p>Then <code>doThis</code> will never exist for class <code>\XXX</code> because <code>\XXX</code> isn't where the method was defined.  It would work from your extended class, though:</p>

<pre><code>$var = new YYY();
$var-&gt;doThis = 'CONTENT HERE';
</code></pre>

<p>Keep in mind that (usually) if the vendor plugin instantiates any instances of that class internally, it will always be of instance <code>\XXX</code>, not your <code>YYY</code> class.</p>

<p>Where that wouldn't be true, is if the vendor plugin supports <a href="https://en.wikipedia.org/wiki/Dependency_injection" rel="nofollow noreferrer">dependency injection</a> where they allow you to inject your extended <code>YYY</code> class wherever <code>\XXX</code> would usually be referenced.</p>

CC BY-SA 3.0
2016-12-01T10:37:55.883Z
Thanks Brad - makes sense but still a bit confused about how to achieve this. To provide more detail I am creating a plugin that utilises the FPDI library through composer but want to extend the core functionality with some custom methods and not sure how best to achieve this. 

Hayelp!
Cole Henley
CC BY-SA 3.0
2016-11-30T21:55:11.390Z
2016-11-30T21:55:11.390Z
Brad Bell
<p>I'm using composer to import a third-party (vendor) plugin into my own Craft plugin. But can't figure out how I extend the functionality of the imported (vendor) plugin.</p>

<p>For example assuming imported plugin defines a base class of <code>XXX</code> I can try to extend this with <code>class YYY extends \XXX</code> but not sure where I save this so I can access this in my code, eg I want to extend the base plugin through a class, eg</p>

<pre><code>class YYY extends \XXX
{
    public function doThis($str) { return $str; }
}
</code></pre>

<p>I'd then want to access this in my Craft plugin with</p>

<pre><code>$var = new \XXX();
$var-&gt;doThis = 'CONTENT HERE';
</code></pre>

<p>Obviously grossly simplified but hope you get the gist.
Just can't figure out how to achieve above or where I should save the code that is extending the vendor class?</p>

<p>Any thoughts?</p>

<plugin-development><extends><classes>
Plugin Development: extending a vendor plugin
# Plugin Development: extending a vendor plugin

(Asked by: username_0 on 2016-11-30)

I'm using composer to import a third-party (vendor) plugin into my own Craft plugin. But can't figure out how I extend the functionality of the imported (vendor) plugin.
For example assuming imported plugin defines a base class of XXX I can try to extend this with class YYY extends \XXX but not sure where I save this so I can access this in my code, eg I want to extend the base plugin through a class, eg
class YYY extends \XXX
{
    public function doThis($str) { return $str; }
}

I'd then want to access this in my Craft plugin with
$var = new \XXX();
$var->doThis = 'CONTENT HERE';

Obviously grossly simplified but hope you get the gist.
Just can't figure out how to achieve above or where I should save the code that is extending the vendor class?
Any thoughts?

**Tags:** plugin-development, extends, classes

---

## 1 answers

### Answer by username_1 on 2016-11-30. Score: 2

In your example, if you do this:
$var = new \XXX();
$var->doThis = 'CONTENT HERE';

Then doThis will never exist for class \XXX because \XXX isn't where the method was defined.  It would work from your extended class, though:
$var = new YYY();
$var->doThis = 'CONTENT HERE';

Keep in mind that (usually) if the vendor plugin instantiates any instances of that class internally, it will always be of instance \XXX, not your YYY class.
Where that wouldn't be true, is if the vendor plugin supports dependency injection where they allow you to inject your extended YYY class wherever \XXX would usually be referenced.

> **Comments:**
> **username_0**: Thanks Brad - makes sense but still a bit confused about how to achieve this. To provide more detail I am creating a plugin that utilises the FPDI library through composer but want to extend the core functionality with some custom methods and not sure how best to achieve this.   Hayelp!  

---

