Kohana 3模块结构问题

发布于 2024-09-17 22:50:25 字数 839 浏览 8 评论 0原文

大家!我有一个关于 Kohana 3 的新问题,或者更确切地说是关于模块结构的新问题。我开发了一个名为 Textblock 的小模块。它是关于普通页面或站点布局的小插入(例如问候语或口号、公司名称)。它包含控制器和模型。模型继承 Sprig_MPTT。我想要实现的一个功能是可以像这样调用这个模块:

$textblock = Textblock::get_single(1);      //by id
$children  = Textblock::get_children_of(4); //id of parent

而不是

$textblock = Sprig::factory('Textblock')->get_single(1);
$children  = Sprig::factory('Textblock')->get_children_of(4);

这些方法在 Model_Textblock 类中定义为 static

因此,我创建了一个包装类 Textblock,它继承 Model_Textblock。例如,如果我突然想将 Sprig 更改为 Jelly,该怎么办?前景根本不会改变。恕我直言,另一个优点是对于任何想要使用此模块的人来说都更加清晰(例如,它可能是团队中的另一个程序员)。

但有人怀疑我是否走错了路......所以,问题本身:建议的是组织我的模块的正确方法吗?或者最好在需要 Textblock 功能的地方保留普通的 Sprig::factory('Textblock') ,删除额外的包装类并删除 static

everybody! I have a new question about Kohana 3, or rather about a module structure. I develop a small module called Textblock. It's about an ordinary page or a small insertion to the site layout (e.g. a greeting or a slogan, company name). It contains both controllers and models. Models inherit Sprig_MPTT. And one feature I'd like to implement is one could be able to call this module like this:

$textblock = Textblock::get_single(1);      //by id
$children  = Textblock::get_children_of(4); //id of parent

and not

$textblock = Sprig::factory('Textblock')->get_single(1);
$children  = Sprig::factory('Textblock')->get_children_of(4);

Those methods are defined in Model_Textblock class as static.

So, I made a wrapper class Textblock, that inherits Model_Textblock. What if I suddenly want change Sprig to Jelly, for example? Foreground won't change at all. Another advantage, imho, is more clarity for anyone, who wants to use this module (e.g. it could be another programmer in the team).

But there's a doubt if I'm on a wrong way... So, the question itself: is the suggested a right way to organize my module? Or it's preferable to keep ordinary Sprig::factory('Textblock') where Textblock's functionality is needed, remove additional wrapper class and remove static?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜夜流光相皎洁 2024-09-24 22:50:25

无需扩展Model_Textblock。您可以创建一个模型实例并调用其方法:

class Textblock {
   public static function get_single($id)
   {
      return Sprig::factory('textblock')->get_single($id);
   }
   // etc
}

但是这样您应该在静态类中复制模型方法(而不是 DRY)。另外,如果您有多个模型怎么办?你想要的(据我所知)就是轻松更改 AR 驱动程序。所以我更喜欢这种类:

class Textblock {   

   // saved objects, dont create on model twice
   protected static $_instances = array();

   public static function model($name)
   {
      if (! isset(self::$_instances[$name]))
      {
          $model = Sprig_MPTT::factory($name);
          // you can add try..catch to prevent exceptions
          // or add another checks
          self::$_instances[$name] = $model;
      }
      return clone self::$_instances[$name];
   }
}

并像 Textblock::model('textblock')->get_single($id) 一样使用它。

There is no need to extend Model_Textblock. You can create a model instance and call its method:

class Textblock {
   public static function get_single($id)
   {
      return Sprig::factory('textblock')->get_single($id);
   }
   // etc
}

But this way you should copy model methods in your static class (not DRY). Also, what if you have more than one model? All you want (as I understand) is to easily change AR driver. So I'd preffer this kind of class:

class Textblock {   

   // saved objects, dont create on model twice
   protected static $_instances = array();

   public static function model($name)
   {
      if (! isset(self::$_instances[$name]))
      {
          $model = Sprig_MPTT::factory($name);
          // you can add try..catch to prevent exceptions
          // or add another checks
          self::$_instances[$name] = $model;
      }
      return clone self::$_instances[$name];
   }
}

and use it like Textblock::model('textblock')->get_single($id).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文