如何在CakePHP 2.0的控制器中导入类? [英] How to import a class in a controller of CakePHP 2.0?

查看:112
本文介绍了如何在CakePHP 2.0的控制器中导入类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CakePHP。我创建了一个外部类,它既不是模型也不是控制器。该类的结构如下:

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";
   }
}

我将该类保存在App-> Lib目录中,并将其命名为UploadImage.php

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

我想在控制器中调用方法 sayHello()

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:


错误:在非对象上调用成员函数sayHello()

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


推荐答案

App :: uses()是您放置在文件开头的语句

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

必须使用php5编程-这意味着您必须使用 new

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

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

并在您的方法中:

$test = new UploadImage();

这篇关于如何在CakePHP 2.0的控制器中导入类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆