静态方法还是不行? [英] Static methods or not?

查看:121
本文介绍了静态方法还是不行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用PHP开发一个小型CMS,现在我正在尝试找出结构。

I need to develop a small CMS using PHP, and right now I'm trying to figure out the structure.

CMS将使用一组功能。例如数据库函数,缓存内容,国际化和类似的东西。

The CMS will be generated using a set of functions. Things like database functions, caching thingies, internationalization and stuff like this.

我想这样做:


  • 使函数非静态方法成为大型网站类的一部分;这样我可以运行该类的多个实例。

  • make the functions non-static methods part of a big "site" class; that way I could run multiple instances of that class. Not sure I would need to do that though..

或使用静态方法将函数拆分为单独的类

or split the functions into separate classes with static methods

这里的主要问题是CMS应该能够管理多个小型网站,而不只是一个。所以要么我使所有的方法静态和添加一个站点切换功能,或使它们正常的对象,我实例化基于我想要管理的网站

The main problem here is that the CMS should be able to manage multiple small sites, not just one. So either I make all methods static and add a "site switch" function, or make them normal objects which I instantiate based on the site which I want to manage

哪个这些是最好的选择?

Which of these would be the best option?

推荐答案

我肯定建议使用静态类。去这个路由将为所有的函数创建一个伪命名空间,所以你不必担心冲突的函数名等,它也阻止你不得不传递一个helper类的实例只是调用你的一个帮助函数。

I'd definitely suggest using static classes for this job. Going this route will create a pseudo namespace for all of your functions so you don't have to worry about conflicting function names, etc, and it also prevents you from having to pass around an instance of your helper class just to call one of your helper functions.

这篇关于静态方法还是不行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆