我有几个关于PHP类的问题,但不能单独询问,所以我在这里:
我有一个我正在构建的CMS,其核心是我的baseClass,logger,DB和模块类.DB和logger是静态类,因此我可以在其他对象中使用它们.有很多模块类加载到baseClass中:
class baseClass {
private $_modules;
public function __construct() {
logger::log("instance created.");
$this->_modules = Array()
}
public function __destruct() {
foreach($this->_modules as $name) $this->unloadModule($name);
logger::log("instance destroyed.");
}
public function loadModule($name) {
if (class_exists($name)) {
if (!isset($this->$name)) {
$this->_modules[] = $name;
$this->$name = new $name();
logger::log("module '$name' loaded.");
}
else logger::log("module '$name' already loaded.", 2);
}
else logger::log("module '$name' does not exist.", 3);
}
public function unloadModule($name) {
if (isset($this->$name)) {
unset($this->$name);
foreach($this->_modules as $id => $mod_name) {
if ($name==$mod_name) {
unset($this->_modules[$id]);
logger::log("module '$name' unloaded.");
}
}
}
else logger::log("module '$name' not loaded.", 2);
}
public function listModules() {
return $this->_modules;
}
}
$instance = new baseClass();
$instance->loadModule('moduleX');
$instance->loadModule('moduleY');
Run Code Online (Sandbox Code Playgroud)
问题1:我的loadModule实现是一个好主意还是你有一个更好的解决方案.或者是否可以反过来扩展一个类,所以我不必加载模块,而是将它们的类定义加载到baseClass中,如:
class moduleX reverseExtends baseClass {
public function f1();
public function f2();
}
class moduleY reverseExtends baseClass {
public function f3();
public function f4();
}
$instance = new baseClass();
$instance->f1();
$instance->f2();
$instance->f3();
$instance->f4();
Run Code Online (Sandbox Code Playgroud)
或至少:
$instance->moduleX->f1();
$instance->moduleX->f2();
$instance->moduleY->f3();
$instance->moduleY->f4();
Run Code Online (Sandbox Code Playgroud)
问题2:现在我使用我的记录器和数据库类作为静态类(我用logger ::和DB::)调用它们,因此可以从任何上下文和范围访问它们 - 是否可以实例化它们并仍然在内部使用它们我的baseClass对象,但没有在baseClass中实例化它们,或者在我将要使用的每个类中将它们设置为这样:
public function __construct() {
global $logger_instance;
$this->logger = $logger_instance;
}
Run Code Online (Sandbox Code Playgroud)
我尝试了不同的方法,从使用包装函数将badClass中的原始对象的引用传递给丑陋.当baseClass未设置并且很难通过模块访问时,使用引用会破坏我的logger对象 - $ this-> base-> logger-> log() - 这又需要引用baseClass对象($ this-> base)和在unloadModule上杀死我的baseClass对象...对于包装器函数 - 我希望能够拥有logger对象的多个实例,因此将实例标识符与函数一起传递使其变得非常丑陋和"不真实"的解决方案.
我需要这样的东西:
public function someFuncionInsideSomeClass() {
$logger->('Log something'); //or $logger('Log something') with __invoke()
//$logger here is in the global scope
}
Run Code Online (Sandbox Code Playgroud)
问题3:我希望一个模块能够修改另一个模块的代码.例如,我加载了一个数据库模块,然后我加载一个记录器模块,并希望记录器模块集成到数据库模块代码中,而不必乱用它的原始代码.当然记录器必须处理集成本身(或者更确切地说,我作为编码器).简单的例子:
class tagCreator {
public function createTag($tag,$data) {
$tag1 = $tag;
$tag2 = '/'.$tag;
return "<$tag1>$data<$tag2>";
}
}
class styleFunctionality {
public function __construct() {
//if class is loaded (tagCreator) | load class (tagCreator)
//do some magic here to tagCreator
//tagCreator -> createTag -> add argument $style
//tagCreator -> createTag -> $tag1 += ' style="'.$style.'"'
//else issue error tagCreator does not exist
}
}
$instance = new baseClass();
$instance->loadModule('tagCreator');
echo $instance->createTag('span','some string');
// returns <span>some string</span>
$instance->loadModule('styleFunctionality');
echo $instance->createTag('span','some string','font-weight: bold');
// returns <span style="font-weight: bold">some string</span>
Run Code Online (Sandbox Code Playgroud)
注意:我不一定是指函数重载,我可能只想更改方法中的一些代码
注意2:调用$ instance-> createTag()不是错误 - 我打算为baseClass 编写一些花哨的 __ call()函数来从其加载的模块中选择方法
对于长篇主题感谢并感谢您阅读本文,我甚至欣赏我的一个问题的答案:)
小智 3
对于 DB 和 Logger,我会考虑使用 Singleton 模式。
<?php
class Singleton {
private static $instance;
private function __construct() {
// no-op
}
/**
* Create the instance or return the instance if it has already been created.
*
* @return object
*/
static public function get_instance() {
if (!isset(self::$instance)) {
$c = __CLASS__;
self::$instance = new $c;
}
return self::$instance;
}
}
?>
Run Code Online (Sandbox Code Playgroud)
一般来说,您可能想要查看需要 PHP5 的 PHP 框架,例如 Kohana。而且,就模式和 PHP 而言,您可能需要查看“PHP 对象、模式和实践”。