【问题标题】:How to call custom component in CakePHP 2.0 versionCakePHP 2.0 版本中如何调用自定义组件
【发布时间】:2012-03-21 02:08:02
【问题描述】:

我正在使用 2.0 版本并尝试调用在我的自定义组件文件中定义的方法。它抛出的致命错误:

致命错误:在非对象中调用成员函数 doComplexOperation()

app/Controller/DepartmentsController.php

<?php
class DepartmentsController extends AppController {
 public $component = array('ArrayToObject');

 function display() {
   $this->ArrayToObject->doComplexOperation(1,2);
 }
}
?>

这是我的组件文件:

app/Controller/Component/ArrayToObjectComponent.php

<?php
class ArrayToObjectComponent extends Component {
  public function doComplexOperation($amount1, $amount2) {
    return $amount1 + $amount2;
  }
}
?>

我是新手,请帮忙。提前致谢

【问题讨论】:

    标签: cakephp controller components call custom-component


    【解决方案1】:

    @dhofstet 说了什么。

    还有:

    <?php
    App::uses('Component', 'Controller');
    class ArrayToObjectComponent extends Component {
        public function doComplexOperation($amount1, $amount2) {
            return $amount1 + $amount2;
        }
    }
    

    【讨论】:

      【解决方案2】:

      您的代码中有一个错字:它应该是 $components 而不是控制器中的 $component

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-12-26
        • 1970-01-01
        • 1970-01-01
        • 2012-01-16
        • 1970-01-01
        • 2017-09-14
        • 2012-07-06
        • 2016-10-09
        相关资源
        最近更新 更多