CakePHP中加载其它类的三种方法比较

CAKEPHP如何引入第三方类 http://bloodbath2012.com/archives/88
1.无耻引入法
此方法打破了CAKEPHP的引用机制,属于强行介入,对于所有古怪类均通用
将引入的类文件放入到CAKEPHP的类库文件夹中/app/Vendor
然后在需要引入的地方加上下面这句
App::import('Vendor', 'nusoap');//我这里引入的类文件名是nusoap.php

然后就可以实例化引入类了
$client = new soapclient('http://localhost:8080/cmpc/services/CMPCTransformService?wsdl',true);


2.温柔引入法
这类方法就需要严格遵守CAKEPHP的语法规则了。类文件名,类名大小写都要符合。
常用的有
App::uses('PostsController', 'Controller');//第一个参数是类名,第二个参数是类归属1
App::load(string $className);//参数是类名,效果和uses类似




CakePHP中加载其它类的三种方法比较
http://blog.csdn.net/hb01846/article/details/6527793
在CakePHP中,如果你想调用其它与本Model或Controller不相关的类,主要有三种方法:

App::import()     ClassRegistry::init()   Controller::loadModel()

这三种方法有什么区别呢?



Posting for, someone searched for terms “cakephp what is the difference between app:import model and loadmodel” and “cakephp what is the difference between app:import and loadmodel” and landed on my blog page but unfortunately i could not provide the answer that time, perhaps. I hope it helps future visitors with similar queries.

Okay, here is the difference. Although the differences has been listed at CakePHP cookbook at cakephp.org here already but i shall try to explain them with the help of short examples.

App::import()

App::import() only includes the file. It does something like we do in include (‘./myfile.php’) and it leaves the rest on you. You can further create an object or use function/methods according to your needs. For example, when you do App::import(‘model’, ‘User’) it includes theuser.php model class file and then you do $User = new User() to create an instance of classUser which is found in user.php file. As an another example, you do App::import(‘helper’, ‘Time’) and it includes helper class file time.php and you do $time = new TimeHelper() to create an instance of TimeHelper class which is found in time.php file. The first argumentpassed to import method is the identification mark for the CakePHP internals and is used to identify the location of the requested file. In this example the string ‘model‘ or ‘helper‘ helps it to search the asked file within ‘models‘ or ‘views/helpers‘ directories respectively.

ClassRegistry::init()

ClassRegistry::init() loads the file and adds the instance to an object map and returns the instance. This is an easy and convenient way to access models. It is helpful when you need the model object to be used one time. For example, you can do ClassRegistry::init(“User”)->find(“all”) and forget about the model User further.

Controller::loadModel()

You can use Controller::loadModel() inside your controller actions for multiple use. It is the mostly used and convenient way and of course the most preferred way to include models. It includes the model class file and adds the model instance as a property of the current controller. For example, you are in PostsController and you do $this->loadModel(‘User’) so now you have User object added as $this->User automatically in the scope following this very line which you can use until your controller script processing ends.

Listing them priority-wise one would suggest you to use them in reverse order i.e. from the most preferred to the least preferred like this:
Controller::loadModel()
ClassRegistry::init()
App::import()

And now perhaps you know why. Do you?

上面是英文,你要是看不懂,直接看我的代码:(演示在Task控制器类中调用类User)



<?php

class TasksController extends AppController {

	var $name = 'Tasks';

 

	function index()

	{

		$this->set('tasks',$this->Task->find('all'));

 

		/**

		* 引用外部不相关类的第一种方法 ClassRegistry::init()

		* 功能:包含一个类文件,生成对象并返回对象。

		$users = ClassRegistry::init("User")->find('all');

		$this->set('users',$users);

		*/

 

		/**

		* 引用外部不相关类的第二种方法 Controller::loadModel()

		* 直接在控制器中调用Model类,并初始化成$this->Model名

		$this->loadModel("User");

		$users = $this->User->find('all');

		$this->set('users',$users);

		*/

 

		/**

		* 引用外部不相关类的第三种方法 App::import()

		* 只相当于include一个类文件,初始化及调用方法都要重新来。

		App::import('model','User');

		$User = new User();

		$this->set('users',$User->find('all'));

		*/

	}

}

?>


注意上面代码中引用其它类时的加红的单引号和双引号,

你可能感兴趣的:(cakephp)