php - 我应该使用 App::import ('Model' , ...) 还是 ClassRegistry(...)?

标签 php cakephp frameworks cakephp-1.3

由于其他答案 ( like this ),我只是想澄清应该在 CakePHP 1.3 中使用什么。

具体来说,我有一种情况需要一个模型依赖另一个模型,所以我想从那个模型中的一个方法加载另一个模型,对信息做一些事情,等等。

documentation for the App Class说:

In previous versions there were different functions for loading a needed class based on the type of class you wanted to load. These functions have been deprecated, all class and library loading should be done through App::import() now.

我假设这涵盖了 ClassRegistry 等的使用,但我只是想让它清楚、确定:

我应该使用 App::import('Model', ...) 从另一个模型或其他模型中利用一个模型吗?如果还有别的什么?

最佳答案

看来,即使是自 2008 年以来的两年,最好的方法还是使用 ClassRegistry::init(),尽管有引用的文档。

这在特定类/方法的实际 API/文档中很明显。

App::import()

Finds classes based on $name or specific file(s) to search. Calling App::import() will not construct any classes contained in the files. It will only find and require() the file.

ClassRegistry::init()

Loads a class, registers the object in the registry and returns instance of the object.

Examples Simple Use: Get a Post model instance ClassRegistry::init('Post');

如您所见,即使是 API 文档也指出了使用 ClassRegistry 加载模型并为您实例化它们的示例,而不是 App::import(后者做的少得多),尽管 CakePHP“书籍”文档中的措辞发生了变化。

关于php - 我应该使用 App::import ('Model' , ...) 还是 ClassRegistry(...)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5237933/

相关文章:

php - CakePHP Controller 将图像保存为 pdf

shell - 在后台运行 CakePHP Shell

PHP PDO 多图像插入

PHP PDO 从数据库中选择并显示在特定的 div 中

cakephp - 你能从 CakePHP 的 View 中调用 Controller 函数吗?

iOS 8 照片套件。从 iCloud 照片共享相册获取最大尺寸的图像

iphone - 简单的(SQLite-)数据库框架

iphone - 如何将架构armv7、armv7s添加到{name}.framework?

PHP MySQL 使用条件连接连接两个表

php - OSCommerce 结帐成功页面跟踪