强制包含 Yii 框架中文件夹中的所有类

发布于 2024-09-14 01:33:22 字数 252 浏览 3 评论 0原文

我可以通过以下方式强制包含一个模型:

Yii::import("application.models.modelName", true);

我应该怎么做才能包含模型目录中的所有模型?

这是我尝试过的:

Yii::import("application.models.*", true);

它不起作用,因为 Yii 仅在需要使用时才会导入。

I can force include one model by:

Yii::import("application.models.modelName", true);

What should I do to include all models from the models directory?

Here's what I've tried:

Yii::import("application.models.*", true);

It doesn't work because Yii will import only when there is a need to use it.

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

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

发布评论

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

评论(2

彼岸花似海 2024-09-21 01:33:22

真的,除了使用它们之外,为什么还要导入它呢?

自动加载 php 功能是无价的:)

Really, why would you want to import it except to use them?

Autoloading php feature is priceless :)

盗琴音 2024-09-21 01:33:22

易伊做不到这一点。与真正的包含相比,导入是相当轻量级的,但如果您必须完成这一点,这里有一个片段:

foreach(glob(Yii::getPathOfAlias('application.models').'/*.php') as $script) {
   require $script;
}

我不怀疑在某些情况下这种方法是最好的,但您确实必须确保它在您的场景中。

Yii can't do that. Importing is pretty lightweight compared to real inclusion, but if you must accomplish that, here's a snippet:

foreach(glob(Yii::getPathOfAlias('application.models').'/*.php') as $script) {
   require $script;
}

I don't doubt there are cases when this approach is the best, but you really have to make sure it is in your scenario.

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