我想在不使用名称空间的Wordpress-Plugin(事件日历专业版)中使用PHP类的功能。 我没有遇到这样的问题,但我担心该插件的开发人员将在未来重新组织他们的插件目录,导致类文件将处于不同的位置=>这意味着我的代码将被破坏。
有没有什么我可以在PHP或Wordpress中做的事情,让我可以在插件目录中找到该文件,以便始终找到它? 或者其他解决方案? 感谢您的任何提示。
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.
最满意答案
只需将autoload.php文件(或其他自动加载机制,如果您不使用composer)添加到您的插件类/文件中,但不要将名称空间添加到它。 作为一个例子,我做了类似于你的目的:
<?php require __DIR__ . '/autoload.php'; use Mailgun\Mailgun; class Mail { private $mail; public function __construct($apiKey) { $this->mail = new Mailgun($apiKey); } }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); } }更多推荐
发布评论