是否只是舒适或有其他目的有一个主要的核心核心?
为什么这样做
$core->A->do();
$core->B->do();
代替
$A->do();
$B->do();
并将所有内容留给课程来满足他们的需求?一旦我们在其中加载不同的类,也不会有一个大对象.
为了解决真正的问题:我目前正在使用依赖注入模式为类提供他们需要的东西,但我也问自己,如果所有类都可以访问资源,那么会不会更好(configs)例如,每次需要时都不调用核心.
这个排序
MyClass
$myclass->get_configs(); // get my configs
$myclass->do_stuff($this->my_configs); // use them
而不是这个
MyCore
$myclass_configs = $this->config->get('configs_for_myclass'); // get configs
$this->myclass = new MyClass($myclass_configs); // call the function and give it the configs
难道这不会避免需要大核心,而且还要分散一切吗?或者它只是疯狂的'沉重的心灵手淫?
编辑:纠正错字.
我写了这两个简单的加密和解密字符串的类:
Encode.php
class Encode {
protected funcion do_encode($string) { .. }
}
Decode.php
class Decode {
protected funcion do_decode($string) { .. }
}
我会做的是:
Encrypt.php
class Encrypt extends Encode, Decode {
protected $stuff_for_parents;
function __construct($configs) {
$this->stuff_for_parents = $configs['SomeConf'];
}
public function encode($string) { $this->do_encode($string); }
public function decode($string) { $this->do_decode($string); }
}
但是我们不能包含多个类,所以:失败.
现在我的问题是:
$encrypt->encode($str); $encrypt->decode($str);