微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 我应该使用App :: import(‘Model’,…)还是ClassRegistry(…)?

由于其他答案( 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“Book”文档中的措辞已经改变了.

原文地址:https://www.jb51.cc/php/138134.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐