What is the best class structure for simple php framework?

What is the best class structure for simple php framework?

乞讨 发布于 2021-11-27 字数 531 浏览 868 回复 3 原文

I am trying to create simple php framework .But i am not sure about class structure for example which classes should extend to which classes .Firsly i am know that some basic classes such as Router , View classes have to access some basic data such as requested url or requested ccontroller and action so how can i import basic data to these classes .If my question is not clear please explain your own experiances and ideas about frameworks.or if you know please talk about known framewoks such as zend , cakephp or symfony

如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。



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


早茶月光 2022-06-07 3 楼

If you've never worked in someone else's framework you will have no idea what ideas work for you and what don't. If you've only ever worked with dis-organised spaghetti code, then you will have some idea of what makes sense for you. If you've made substantial re-organisation to dis-organised spaghetti code in more than one unrelated project, you should have a good idea of how you think in building a framework. :-)

If you don't have a firm idea of how you want to write a framework, but you want to write one, I think you need to start with nothing and build it organically. However, you should have some idea how you want it to look like, even if it's something as silly as where you want the include files to live. In other words, all you need is a starting point. Then it's a matter of re-factoring ugliness as you encounter it. Before long, you will have a database handler, a dispatch mechanism, perhaps a data access layer, just maybe even a templating system!

Responding to @MrFox, though, you should be aware of dangerous dependancies. Obviously a data access layer will depend on a database handler; so what you don't want is for your database handler to depend on your data access layer. In fact, you don't want them to have intimate knowledge of each other, either. It should be a reasonably "black-box" interface.

Also, a number of frameworks try to build "one" object heirarchy. This means they think an Action is-a Model is-a Database Handler. I personally have problems with this mis-applied abstraction, so this is a heads-up to not be afraid to spurn this idea if it doesn't work for you, either. As @Tyndall said, there's no one, single "right" way to do it.

一世旳自豪 2022-06-07 2 楼

There is a simple rule of thumb:

  • Either you have a need that the standard frameworks can not fulfill
  • or you should use a good standard framework that does what you need.

In the first case the restriction will be the driving aspect in your design.

There are no general design rule for class structures in frameworks. But try to keep this in mind, if you decide to write your own one:

  • Minimize dependencies between modules
  • Always try to give default values
  • Try to minimize the overhead
  • Talk a lot with peers about it to find flaws early.
甜嗑 2022-06-07 1 楼

There is no best class structure. Depends on what you are trying to do. Do you mean purest MVC design? Most testable? Most easy to understand?