小编eij*_*eij的帖子

为什么常见的PHP框架使用中央Core?

是否只是舒适或有其他目的有一个主要的核心核心?

为什么这样做

    $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

难道这不会避免需要大核心,而且还要分散一切吗?或者它只是疯狂的'沉重的心灵手淫?

编辑:纠正错字.

php oop frameworks design-patterns

7
推荐指数
2
解决办法
469
查看次数

在一个中使用两个类:设计错误?

我写了这两个简单的加密和解密字符串的类:

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); }
    }

但是我们不能包含多个类,所以:失败.
现在我的问题是:

  1. 这是一个设计问题吗?因为这种情况对我来说并不奇怪,是吗?
  2. 是否有另一种方法可以让一个对象同时使用不同类中的函数?有点$encrypt->encode($str); $encrypt->decode($str);

php design-patterns

1
推荐指数
1
解决办法
147
查看次数

标签 统计

design-patterns ×2

php ×2

frameworks ×1

oop ×1