MaxWidth
MaxWidth

Reputation: 518

Creating an instance of a PHP class in a Wordpress plugin

I want to use the functions of a PHP class in a Wordpress-Plugin (events calendar pro) that does not use namespaces. I am not having a problem doing so but I am scared that the developers of that plugin will reorganize their plugin-directory in the future with the result that the class-file will then be at a different location => meaning my code will break.

Is there anything that I can do in PHP or Wordpress that allows me to locate that file in the plugin-directory so that it will always be found? Or any other solution? Thanks for any hint.

Upvotes: 0

Views: 81

Answers (1)

meysam
meysam

Reputation: 1794

Just add autoload.php file (or other autoload mechanism if you don't use composer) to your plugin class/file, but don't add the namespace to it. As an example I did this similar to your purpose:

<?php

require __DIR__ . '/autoload.php';
use Mailgun\Mailgun;

class Mail
{
    private $mail;

    public function __construct($apiKey)
    {
        $this->mail = new Mailgun($apiKey);
    }
}

Upvotes: 1

Related Questions