CodeIgniter在同一个类中调用函数

时间:2010-10-29 04:12:55

标签: codeigniter

我正在尝试在我的CodeIgniter应用程序中执行此代码:

<?php
  class Inventory extends Controller {

    function current_stock()
    {
      //do something
    }

    function add_stock()
    {
      //do something-else
      ****then do function current_stock()*****

    }

  }

如何在第二个函数中执行另一个函数? 概述here(关于扩展控制器)的方法对我来说太过分了。

我错过了一个更简单的方法吗?

3 个答案:

答案 0 :(得分:100)

好的,我同意这是一个主要的蠢事;来自缺乏OOP理解;

<?php
class Inventory extends Controller {
    function current_stock() {
        //do something
    }

    function add_stock() {
        //do something-else
        $this->current_stock();
        // and we called the other method here!
    }
}

只是我没想到它会这么容易

答案 1 :(得分:7)

只需使用$this->your_function_name();

即可

答案 2 :(得分:2)

只有$ this-&gt; nameFunction();
示例

<?php 

class Hello extends CI_Controller{

 public function index(){
  $this->hello();
 }
public function hello(){
  return "hello world";
 }
}
相关问题