How to import a class in a controller of CakePHP 2.0?

爷,独闯天下 提交于 2019-12-11 02:24:17

问题


I am using CakePHP. I created an external class which is not a model nor a controller. The structure of the class looks like this

class UploadImage{
    function sayHello(){
       return "hahaha";
   }
}

I saved the class in the App->Lib directory and named it as UploadImage.php

I wanted to call the method sayHello() in my controller which is:

class ContentsController extends AppController {

    public $helpers = array('Html', 'Form');

    public function index() {
        $test = App::uses('UploadImage','Lib');
        debug($test->sayHello());
    }
}

Now when I run the above page, I get the following error:

Error: Call to a member function sayHello() on a non-object


回答1:


App::uses() is a statement you place at the beginning of the file

you still have to program in php5 - meaning that you have to use new!

App::uses('UploadImage','Lib');
class ContentsController extends AppController {}

and in your method:

$test = new UploadImage();


来源:https://stackoverflow.com/questions/11339680/how-to-import-a-class-in-a-controller-of-cakephp-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!