CodeIgniter Forums
calling a function in a library - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: calling a function in a library (/showthread.php?tid=27501)



calling a function in a library - El Forum - 02-12-2010

[eluser]stef25[/eluser]
How do I call a function in a library from another function in that same library? I have my $CI =& get_instance(); and the function I want to call is say test(). Ive tried calling it via test(), $this->test(), $CI->test(), $CI->lib_name->test() ...

?


calling a function in a library - El Forum - 02-12-2010

[eluser]danmontgomery[/eluser]
Code:
$this->test();



calling a function in a library - El Forum - 02-12-2010

[eluser]stef25[/eluser]
That gives a Call to undefined function error.


calling a function in a library - El Forum - 02-12-2010

[eluser]danmontgomery[/eluser]
Then you're doing something else wrong, post some code.


calling a function in a library - El Forum - 02-12-2010

[eluser]stef25[/eluser]
Code:
class my_lib {
    
function get_locations($location, $filter_array){
...
$nbr_matches = $this->count_key($test, 'full-name');
...
}

function count_key($array, $key) {
  $count = 0;
  foreach($array as $k => $val) {
    if($k == $key)
    $count++;
    if(is_array($val))
    $count += count_key($val, $key);
  }
  return $count;
  }
}



calling a function in a library - El Forum - 02-12-2010

[eluser]danmontgomery[/eluser]
Code:
$count += count_key($val, $key);
Should be
Code:
$count += $this->count_key($val, $key);



calling a function in a library - El Forum - 02-12-2010

[eluser]stef25[/eluser]
Great catch, thanks. I was looking at the wrong line number.


calling a function in a library - El Forum - 02-12-2010

[eluser]danmontgomery[/eluser]
Wink Cheers