在CakePHP中导入自定义类

ken*_*xit 1 php cakephp

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

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

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

}

?>
Run Code Online (Sandbox Code Playgroud)

但问题是,包括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";
    }
}
?>
Run Code Online (Sandbox Code Playgroud)

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

cor*_*elb 5

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

App::import('Vendor', 'custom2');
Run Code Online (Sandbox Code Playgroud)

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