PHP:在我自己的 MVC 框架中包含文件?

发布于 2024-10-03 11:08:36 字数 546 浏览 1 评论 0原文

我刚刚开始构建我自己的 MVC 框架。从头开始了解一切并且只获取我的应用程序真正需要的东西感觉很好。

我有 codeIgniter 背景,这帮助我进入 MVC 视角来看待事物。在 codeigniter 中,要包含文件,使用 codeIgniters 自己的加载类

该加载类在加载文件时会检查以前是否包含过该文件,如果没有包含过该文件,则可以确保文件不会被包含两次。

然而,这种包含文件的方法有一个缺点,即无法(?)在我的文件上运行测试或利用我需要的 IDE 中的 PHPdoc。

显然,我可以使用正常的 include & PHP 中的 require 函数在我的应用程序中前后移动,其中需要某个库,但显然可能两次包含相同的文件不太好......

所以 - 什么是在我自己的 PHP5 MVC 框架中使用包含文件的好解决方案吗?

I've just started to build my very own MVC framework. Feel's kind of nice to know everything from the ground up and only get the stuff that's really necessary for my app.

I come from a codeIgniter background which helped me to get into the MVC perspective of seeing things. In codeigniter, to include a file, codeIgniters very own load class is used.

This load class, when loading a file, checks if a file have previously been included, and if not includes it, which ensures that a file isn't included twice.

However this method of including files has the downside of making it impossible (?) to run tests on my files or take advantage of PHPdoc in my IDE, which I need.

Clearly, I can use the normal include & require functions in PHP forwards and backwards across my application, where a certain library would be needed, but it clearly won´t be good to possible include the same file twice...

So - what's a good solution to use in my own PHP5 MVC framework to include files?

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

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

发布评论

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

评论(4

欢你一世 2024-10-10 11:08:37

我正在做与你类似的事情,并且我正在使用 autoload

只需将其放入index.php:

function __autoload($class_name) {
    include $class_name . '.php';
}

或包含检查多个目录所需的任何逻辑。

编辑:这是我的(稍微不稳定的)代码,用于检查备用路径。如果您需要检查多个路径,则可以更干净地完成。

function __autoload($class_name) {
  $path = INC_PATH . strtolower($class_name) . '.php';

  if (!file_exists($path)) {
    $path = MODELS_PATH . strtolower($class_name) . '.php';
  }

  require $path;
}

I'm doing a similar thing to you, and I'm using autoload.

Just put this in index.php:

function __autoload($class_name) {
    include $class_name . '.php';
}

or include whatever logic you need to check multiple directories.

Edit: Here's my (slightly flaky) code for checking an alternate path. It could be done a lot more cleanly if you were going to need to check multiple paths.

function __autoload($class_name) {
  $path = INC_PATH . strtolower($class_name) . '.php';

  if (!file_exists($path)) {
    $path = MODELS_PATH . strtolower($class_name) . '.php';
  }

  require $path;
}
明月松间行 2024-10-10 11:08:37

只需使用 include_oncerequire_once 。虽然 CI 有一个加载类,但您绝不需要使用它。

just use include_once and require_once . while CI has a load class, you're by no means required to use it.

听风吹 2024-10-10 11:08:37

使用 include_once/ require_once 但最好的解决方案是使用自动加载器

http://php .net/manual/de/language.oop5.autoload.php

你使用什么IDE?

use include_once/ require_once but the best solution would be working with a autoloader

http://php.net/manual/de/language.oop5.autoload.php

what IDE do you use?

话少情深 2024-10-10 11:08:37

一般来说,php 有内置的 include_once ,顾名思义,它包含该文件,但只包含一次。

或者您可以使用大多数 IDE 支持的 自动加载(尽管有些需要一点提示 - 取决于您使用的 IDE)。

Generally php has the built in include_once which as the name says includes the file, but only once.

Or you can use autoloading which most IDEs support (though some need a bit of a hint - depends on what IDE are you using).

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