在Wordpress插件中创建一个PHP类的实例(Creating an instance of a PHP class in a Wordpress plugin)

编程入门 行业动态 更新时间:2024-10-22 16:44:11
在Wordpress插件中创建一个PHP类的实例(Creating an instance of a PHP class in a Wordpress plugin)

我想在不使用名称空间的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); } }

更多推荐

本文发布于:2023-08-04 16:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1417686.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:创建一个   插件   实例   PHP   Wordpress

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!