在CakePHP中导入自定义类

时间:2014-12-01 13:36:05

标签: php cakephp

我有2个自定义PHP类放在Lib文件夹中。在Controller中,我需要导入以下类Custom2,它正在引入类Custom。我可以通过实例化此类并在Controller的操作中调用其方法来使用类Custom2,如下所示:

<?php
App::import('Lib', 'custom2');
class TestController extends AppController {

    function index() {
         $custom = new Custom2();
         $test = $custom->test();
    }

}

?>

但问题是,Custom.php内的Custom2.php包括include_once无效。我正在使用这个实现:

Custom2.php:

<?php
include_once 'Custom.php';

class Custom2 {
    public function test() {
         $test = new Custom(); // this is not working               
    }
}
?>
Custom.php:

<?php

class Custom {
    public function test() {
        return "test";
    }
}
?>

两个类都在同一个文件夹(Lib)中。这有什么不对?

1 个答案:

答案 0 :(得分:5)

您可以将它们放在Vendor文件夹中,如documentation中所述。

App::import('Vendor', 'custom2');

另一种选择是放入Lib文件夹