Jer*_*ris 35 php oop wordpress
我正在使用WordPress插件,我正在努力确保最佳实践.我有两个类,我的插件类"Jargonaut"是必需的,然后是另一个名为"Dictionary"的类,它包含require_once()
在我的主插件文件中.
Jargonaut类中的大多数代码都处理初始化并提供类似控制器的功能,但其中大部分都高度依赖于使用Dictionary对象(即根据我对术语的理解紧密耦合).我希望将Dictionary类保持分离,因为它更像是模型(在MVC架构中)并且与我的数据库接口.
我看到紧密与松散耦合中有很多灰色区域,我很难决定多少太多了?
hak*_*kre 60
如果你的插件需要字典对象,它必须要求它:
class MyPlugin
{
/**
* @var Dictionary
*/
private $dictionary;
private function __construct(Dictionary $dictionary)
{
$this->dictionary = $dictionary;
}
Run Code Online (Sandbox Code Playgroud)
你现在已经将你的插件松散地耦合了Dictionary
,插件类不再负责为自己创建Dictionary,因为它是注入的.它需要它所需要的东西.
那怎么会有用呢?该插件需要在某处创建,因此需要一个工厂.工厂构建方法知道您的插件需要什么:
class MyPluginFactory
{
public static function build($pluginName)
{
$plugin = NULL;
switch($pluginName)
{
case 'MyPlugin':
$dictionary = new Dictionary();
$plugin = new MyPlugin($dictionary);
}
return $plugin;
}
}
Run Code Online (Sandbox Code Playgroud)
由于这是wordpress,我们知道插件的引导是通过包含插件文件来完成的.所以在它开始时,需要创建插件.由于包含在全局范围内完成,我们希望将插件对象保留在内存中,但可能不作为全局变量提供.这并不妨碍您创建多个插件实例,但它将确保当wordpress初始化您的插件(加载您的插件)时,它将仅使用该单个实例.这可以通过使插件工厂有一些额外的功能来完成:
class MyPluginFactory
{
...
public static $plugins;
public static function bootstrap($pluginName)
{
$plugin = self::build($pluginName);
self::$plugins[] = $plugin;
return $plugin;
}
Run Code Online (Sandbox Code Playgroud)
请注意,静态类成员变量的唯一用法仅是确保插件保留在内存中.它在技术上是我们通常想要防止的全局变量,但是,实例需要存储在某个地方,所以在这里(我将其更改为public,因为它是一个全局变量,它不应该对此感到害羞.公众可以在某些私人或受保护限制过多的情况下提供帮助.此外,它也不应成为问题.如果是问题,还有另一个问题需要先解决).
这基本上将你的插件代码与wordpress本身分离.你可能还想创建一个类,它提供和接口你正在使用的任何wordpress函数,所以你不直接绑定这些函数,你的插件代码保持干净,松散地耦合到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.
}
...
}
Run Code Online (Sandbox Code Playgroud)
如果您的插件需要WordpressSystem
执行任务(通常是这种情况),请再次将其添加为依赖项:
class MyPlugin
{
...
public function __construct(WordpressSystem $wp, Dictionary $dictionary)
...
Run Code Online (Sandbox Code Playgroud)
所以要最终包装它,只需要插件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;
}
...
Run Code Online (Sandbox Code Playgroud)
引导方法还可以注册自动加载器或执行要求.
希望这很有用.