1

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?

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.

  • 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 Dec 1 '16 at 10:37

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.