0

Im not so experienced in php , Im using codeigniter to write my application , I have my own library and within my library there are three functions/methods that passes there arguments in one function which is in one of my models , my question is how will i manipulate/trick the method in my model to know exactly which function among the three in my library has passed the value and return the correct value ..

1st function

 public function id_exist ($id) {

      if(empty($id)) {
           return FALSE;
       }
      return $this->library_model->this_exist($id);
   }

2nd function

 public function group_exist($group) {

       if(empty($group)){

           return FALSE;
       } 

       return $this->library_model->this_exist($group);  

   }

with the 3rd same as the above 2

in my model

 public function this_exist ($item) {

              if(empty($item) || !isset($item)) {

                  return FALSE;

              }

         // here is where i need to know which function has passed the argument so that i can work with it and return the correct value from the database 


 }
JohnMi
  • 81
  • 1
  • 7
  • Check out this question: http://stackoverflow.com/questions/190421/caller-function-in-php-5 – Max Dec 01 '13 at 18:11
  • I kinda bumped into that question before posting this question . can you please illustrate or give out an example how i will trace methods from other classes using [debug_backtrace](http://uk.php.net/manual/en/function.debug-backtrace.php) – JohnMi Dec 01 '13 at 18:18
  • There is a perfectly adequate example on that page. http://stackoverflow.com/a/190426/2812842 – scrowler Dec 01 '13 at 18:21

1 Answers1

0

Might be dirty, might be not sophisticated, but why not passing another argument which tells exactly the origin?

public function id_exist ($id) {
      if(empty($id)) {
           return FALSE;
       }
      return $this->library_model->this_exist('id', $id);
   }

public function group_exist($group) {

       if(empty($group)){
           return FALSE;
       } 

       return $this->library_model->this_exist('group', $group);  
   }

Model:

public function this_exist ($origin, $item) {

  if(empty($item) || !isset($item)) {
       return FALSE;
  }

  if($origin == 'id'){
   // do something
  }
  elseif($origin == 'group') {
   // do something else
  }
}
Damien Pirsy
  • 25,319
  • 8
  • 70
  • 77
  • I think this is what i wanted .. using debug_backtrace will be a bit of a headache for me because i do not know exactly how it works .. thanx bro @Damien Pirsy – JohnMi Dec 01 '13 at 18:31