轻量级 PHP 框架的技巧

发布于 2024-09-29 08:37:56 字数 1063 浏览 3 评论 0原文


我正在尝试写下一些关于轻量级框架(在 PHP5 上)的具体想法,目的是处理大量请求并使其在高流量情况下能够很好地扩展。 我已经开始使用它了,但我对编码风格不太满意,它基本上由简单的类和助手组成,有一个主要的类来加载它们。

class Settings {
    // I provide settings for everyone and I'm called by everyone via singleton.
     // $settings = instance(Settings);
}

class Database {
    // I provide the db connection.
}

class Session {
    // I handle the sessions.
}

[...]

class Core {
    // I start every class we need.
}

该结构是一种基本结构。 APP是公共树,SYSTEM是私有树。

app
 |-- css
 |-- js
 |-- page_name
       |-- index.php
 [...]

system
 |-- settings
 |-- libs
 |-- helpers
 |-- langs
 [...]

基本上我所做的就是在 index.php 文件的常量中设置路径,该文件调用 boot.php 文件,其中包含我们需要的库(设置、数据库、Session),然后包含使用对象启动每个类的 core.php 文件。 当然,这是一个综合。
我设计的一切都更加独立于核心,所以如果我需要,例如一些设置,我只需引用设置类(通过单例),而不是调用核心,然后获取数据。

主要问题是我不知道这种“风格”是否有利于我的目标,因为与其他主要框架相比,它对我来说看起来很笨拙。我也不明白为什么每个人都使用诸如 Implements、Extends、Interface 或其他东西,我发现这比将所有内容放入具有公共和私有可见性的单个类或仅使用函数更令人困惑。
我可以获得一些高级提示、示例或其他有助于实现我的目标的内容吗?

谢谢!

I'm trying to write down some concrete ideas for a light framework (on PHP5), with the purpose to handle a lot of requests and to make it scale well in terms of high traffic eventualities.
I've already started it, but I'm not really satisfied about the coding style, which is basically composed by simple classes and helpers, with a main one to load them all.

class Settings {
    // I provide settings for everyone and I'm called by everyone via singleton.
     // $settings = instance(Settings);
}

class Database {
    // I provide the db connection.
}

class Session {
    // I handle the sessions.
}

[...]

class Core {
    // I start every class we need.
}

The structure is a kinda basic one. APP is a public tree, SYSTEM is a private tree.

app
 |-- css
 |-- js
 |-- page_name
       |-- index.php
 [...]

system
 |-- settings
 |-- libs
 |-- helpers
 |-- langs
 [...]

Basically what I do is setting the paths in the constants on the index.php file, which calles a boot.php file that includes the libraries that we need (Settings, Database, Session), then includes the core.php file that starts every class using objects.
Of course this is a synthesis.
I designed everything to be more independent from the core, so if I need, for example, some settings, I just refer to the settings class (by singleton), instead recall the core and then get the data.

The main problem is that I don't know if this "style" could be good for my goals, 'cause it looks nooby to me, compared to the others main frameworks. I don't also get why everyone uses things like Implements, Extends, Interface, or others, which I find more confusing than putting everything into a single class with public and private visibility or just using functions.
Can I get some advanced tips, examples, or whatever can help reaching my goals?

Thanks!

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

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

发布评论

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

评论(2

懒的傷心 2024-10-06 08:37:56

你看过CodeIgniter吗?似乎做你正在做的一切。它所做的(看起来你正在尝试做的)是创建一个加载和管理请求的超级对象。

Have you looked at CodeIgniter? Seems to do everything you're doing. What is does (and looks like you're trying to do) is create a super object which loads and manages requests.

如若梦似彩虹 2024-10-06 08:37:56

如果您想扩展,这里有一些比编码结构更重要的提示:

  • 轻松将 PHP 代码作为静态 HTML 缓存到文件中,以避免不必要的数据库查询
  • 使数据库连接轻松使用主/从
  • 配置易于使用 memcached

在您的编码结构成为问题之前,您就需要使用这些类型的东西,至少从流量可扩展性的角度来看是这样。从维护角度来看,这可能会带来更多问题。

If you want to scale, here's some tips that are more important than coding structure:

  • Make it easy to cache PHP code to a file as static HTML to avoid unnecessary DB queries
  • Make it easy for the DB connection to use a master/slave configuration
  • Make it easy to use memcached

You'll need to use these types of things long before your coding structure becomes a problem, at least from a traffic scalability perspective. It may be more problematic from a maintenance perspective.

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