Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter call function within the same class

Tags:

codeigniter

I am trying to do this code in my CodeIgniter application :

<?php
  class Inventory extends Controller {

    function current_stock()
    {
      //do something
    }

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

    }

  }

How do I execute another function from within a second one? The approach outlined here (about extending controllers) is something of an overkill for me.

Am I missing a much easier way?

like image 671
Mr Hyde Avatar asked Oct 29 '10 04:10

Mr Hyde


3 Answers

OK, I agree this is a MAJOR goof-up; comes from lack of OOP understanding;

<?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!
    }
}

Just that I didn"t expect it to be so easy

like image 127
Mr Hyde Avatar answered Nov 07 '22 06:11

Mr Hyde


Just use $this->your_function_name();

like image 26
Rahul Bajaj Avatar answered Nov 07 '22 05:11

Rahul Bajaj


Only $this->nameFunction();
example

<?php 

class Hello extends CI_Controller{

 public function index(){
  $this->hello();
 }
public function hello(){
  return "hello world";
 }
}
like image 4
Daniel Muñoz Avatar answered Nov 07 '22 07:11

Daniel Muñoz