由于其他答案 ( 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/文档中很明显。
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.
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/