首页 > 解决方案 > 使用另一个类的方法,给出错误

问题描述

我正在尝试使用另一个类的方法。这个问题已经被问过好几次了,但在我的情况下,我得到了一个错误。我确定这与我犯的一个错误有关。

class Reports_images extends Reports{
    public function testOG(){
      return('hi there');
    }
}

在另一个文件 Reports.php 中:

require APPPATH.'/controllers/Reports_images.php';

public function appAddPics_post() {
     $bakerboyTest = new Reports_images();
     $bakerboy = $bakerboyTest->testOG();

     $this->response($bakerboy  ,REST_Controller::HTTP_OK);
}

我将 CodeIgniter 与休息控制器一起使用,除此之外一切正常。我试图从另一个控制器中的方法返回一个值。

标签: phpcodeigniter

解决方案


那永远行不通。Reports_Images 类扩展了 Reports 类,同时调用了 Reports 类的 testOG 函数。

这将起作用。报告_images.php:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Reports_images extends CI_Controller{
    public function testOG(){
      return('hi there');
    }
}

然后在 Reports.php 的其他类 Reports 中执行以下操作:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH.'/controllers/Reports_images.php';

class Reports extends Reports_images{
    public function appAddPics_post() {
       $this->response($this->testOG(), REST_Controller::HTTP_OK);
    }
}

推荐阅读