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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突