【问题标题】:CodeIgniter call function within the same classCodeIgniter 在同一个类中调用函数
【发布时间】:2011-05-02 05:25:20
【问题描述】:

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

<?php
  class Inventory extends Controller {

    function current_stock()
    {
      //do something
    }

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

    }

  }

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

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

【问题讨论】:

    标签: codeigniter


    【解决方案1】:

    好的,我同意这是一个重大错误;源于缺乏对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!
        }
    }
    

    只是没想到这么简单

    【讨论】:

    • $this,我是一个有直接调用的 java 人.. :\
    【解决方案2】:

    只需使用$this-&gt;your_function_name();

    【讨论】:

      【解决方案3】:

      只有 $this->nameFunction();
      例子

      <?php 
      
      class Hello extends CI_Controller{
      
       public function index(){
        $this->hello();
       }
      public function hello(){
        return "hello world";
       }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-21
        • 2011-09-19
        • 1970-01-01
        • 2011-10-02
        相关资源
        最近更新 更多