gpt4 book ai didi

php - WordPress 插件 : How do I avoid "tight coupling"?

转载 作者:IT王子 更新时间:2023-10-29 01:02:11 26 4
gpt4 key购买 nike

我正在开发一个 WordPress 插件,并努力确保最佳实践。我有两个类,我的插件类“Jargonaut”是必需的,然后是另一个名为“Dictionary”的类,它随 require_once() 包含在我的主插件文件中。

Jargonaut 类中的大部分代码都涉及初始化并提供类似 Controller 的功能,但其中大部分高度依赖于使用 Dictionary 对象(即根据我对该术语的理解紧密耦合)。我希望将 Dictionary 类分开,因为它更像是一个模型(在 MVC 架构中)并与我的数据库接口(interface)。

我在紧耦合和松耦合中看到很多灰色区域,我很难决定多少是太多了?

最佳答案

如果你的插件需要字典对象,它必须请求它:

class MyPlugin
{
/**
* @var Dictionary
*/
private $dictionary;
private function __construct(Dictionary $dictionary)
{
$this->dictionary = $dictionary;
}

您现在已经将您的插件与 Dictionary 松散耦合,插件类不再负责为自己创建 Dictionary,因为它已被注入(inject)。它需要什么就拿什么。

那它是如何工作的呢?插件需要在某处创建,所以这需要一个工厂。工厂构建方法知道您的插件需要什么:

class MyPluginFactory
{
public static function build($pluginName)
{
$plugin = NULL;
switch($pluginName)
{
case 'MyPlugin':
$dictionary = new Dictionary();
$plugin = new MyPlugin($dictionary);
}
return $plugin;
}
}

由于这是 wordpress,我们知道插件的引导是通过包含插件文件来完成的。所以在开始时,需要创建插件。由于包含是在全局范围内完成的,我们希望将插件对象保留在内存中,但可能不会作为全局变量使用。这不会阻止您创建多个插件实例,但它将确保当 wordpress 初始化您的插件(加载您的插件)时,它将仅使用该单个实例。这可以通过为插件工厂添加一些附加功能来完成:

class MyPluginFactory
{
...
public static $plugins;
public static function bootstrap($pluginName)
{
$plugin = self::build($pluginName);
self::$plugins[] = $plugin;
return $plugin;
}

这里要注意,静态类成员变量的唯一用途只是确保插件保留在内存中。从技术上讲,它是我们通常要阻止的全局变量,但是,实例需要存储在某个地方,所以在这里(我将其更改为 public 因为它一个全局变量,它不应该对此感到害羞。在某些情况下,私有(private)或 protected 限制过于严格时,拥有公共(public)可以提供帮助。此外,这应该不是问题。如果它一个问题,那么应该有另一个问题先修复)。

这基本上将您的插件代码与 wordpress 本身分离。您可能还想创建一个类,为您正在使用的任何 wordpress 函数提供接口(interface),这样您就不会直接绑定(bind)到这些函数,并且您的插件代码保持干净并与 wordpress 本身松散耦合。

class WordpressSystem
{
public function registerFilter($name, $plugin, $methodName)
{
... do what this needs with WP, e.g. call the global wordpress function to register a filter.
}
...
}

如果您的插件需要 WordpressSystem 来执行任务(通常是这种情况),则再次将其添加为依赖项:

class MyPlugin
{
...
public function __construct(WordpressSystem $wp, Dictionary $dictionary)
...

所以最后总结一下,只需要插件 php 文件:

<?php
/*
* MyPlugin
*
* Copyright 2010 by hakre <hakre.wordpress.com>, some rights reserved.
*
* Wordpress Plugin Header:
*
* Plugin Name: My Plugin
* Plugin URI: http://hakre.wordpress.com/plugins/my-plugin/
* Description: Yet another wordpress plugin, but this time mine
* Version: 1.2-beta-2
* Stable tag: 1.1
* Min WP Version: 2.9
* Author: hakre
* Author URI: http://hakre.wordpress.com/
* Donate link: http://www.prisonradio.org/donate.htm
* Tags: my
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
Namespace MyPlugin;

# if your file is named 'MyPlugin.php' this will be 'MyPlugin'.
return PluginFactory::bootstrap(basename($plugin, '.php'));

class PluginFactory
{
private static $plugins;
public static function bootstrap($pluginName)
{
$plugin = self::build($pluginName);
self::$plugins[] = $plugin;
return $plugin;
}
public static function build($pluginName)
{
$plugin = NULL;
switch($pluginName)
{
case 'MyPlugin':
# Make your plugin work with different Wordpress Implementations.
$system = new System\Wordpress3();
$dictionary = new Dictionary();
$plugin = new Plugin($system, $dictionary);
}
return $plugin;
}
}

class Plugin
{
/**
* @var System
*/
private $system;
/**
* @var Dictionary
*/
private $dictionary;
private function __construct(System $system, Dictionary $dictionary)
{
$this->system = $system;
$this->dictionary = $dictionary;
}

...

bootstrap 方法还可以负责注册自动加载器或执行要求。

希望这是有用的。

关于php - WordPress 插件 : How do I avoid "tight coupling"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8688738/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com