douxuqiao6394
2014-12-01 13:36
浏览 59
已采纳

在CakePHP中导入自定义类

I have 2 custom PHP classes placed in Lib folder. In Controller I need to import following class Custom2, which is inporting class Custom. I can use class Custom2 by instantiation this class and invoking its method inside Controller`s action as following:

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

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

}

?>

But problem is, that including Custom.php inside Custom2.php is not working trough include_once. I am using this implementation:

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

Both classes are in the same folder(Lib). What is wrong here?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongluzhi5208 2014-12-01 13:44
    已采纳

    You can put them in the Vendor folder, as described in the documentation.

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

    Another option would be to put in the Lib folder

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题