Welcome Guest, Not a member yet? Register   Sign In
How to call library method from library
#1

[eluser]SneakyDave[/eluser]
I'm using PHP 4.3.0 (don't ask), and CI 1.7.1, and I'm having the following problem.

I have a method in a library that needs to call another method from within the same library to do some processing on an array.

The code looks like this:

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mylibrary{

    function do_something($array_a)
    {
       $CI =& get_instance();
       $array_b = $this->do_something_else($array_a);                  
    }

    function do_something_else($thearray)
    {
          $CI =& get_instance();
          $return $thearray;
          // doesn't do much, just demonstrating.
    }

}

I get a PHP syntax error: "Non-existent class: mylibrary", pointing to the 2nd line in the "do_something" method.

Whether I call the do_something_else() method as "$this->do_something_else()" or "$CI->do_something_else()" or "do_something_else()", I get the same error.

I seem to recall in PHP5, that I can call the method within the library as "$this->do_something_else", but I don't have my notes with me to confirm that at the moment.

What am I doing wrong?
#2

[eluser]pistolPete[/eluser]
Could you please post the full error message?
Btw: Since you do not use any CI related functions / objects you don't need to call get_instance() in every function.
#3

[eluser]SneakyDave[/eluser]
Well, basically, it seems no matter how I call the function/method inside the library, I get this error:
Fatal error: Call to undefined function: do_something_else() in blahblahblah/libraries/mylibrary.php on line xxx

So I still don't know the proper way to call a library method from within the library.

These types of calls don't seem to work:
Code:
$this->mylibrary->do_something_else
$CI->do_something_else
do_something_else
#4

[eluser]InsiteFX[/eluser]
Sounds like you did not load the library first!

Thats why your getting an error, for you to call another function in the library it needs to be loaded by CodeIgniter.

Or you do not have your library name correct.

Also you are missing your libraries Constructor Method!

Enjoy
InsiteFX
#5

[eluser]SneakyDave[/eluser]
I have my library autoloaded. All the functions in the library work from the controller. For whatever reason, I can't call any functions in the library from within the library itself.

Code:
Also you are missing your libraries Constructor Method!
My library doesn't have a constructor method, I'm not passing any data to it. I don't see one is required from the description on this page:
http://ellislab.com/codeigniter/user-gui...aries.html

And this note from the page...
Code:
Also, please note: If you are running PHP 4 it's usually best to avoid calling get_instance() from within your class constructors. PHP 4 has trouble referencing the CI super object within application constructors since objects do not exist until the class is fully instantiated.

Are you saying that I need to load the library from within the library function itself? I'll try that, but I don't think it'll work.

Again, my library functions work fine from a controller. Currently, I have the above problem resolved by calling both functions from the controller, but it's messy.

It is only when I need to call a library function from within the library itself that I can't find the correct syntax. I think it's a PHP4 issue, but I need to go through a few PHP5 projects that I have working to verify that.

And it doesn't seem to be a CI version issue, both 1.7.1 and 1.7.2 act the same way.
#6

[eluser]BrianDHall[/eluser]
Try this:

Code:
class Mylibrary{

private $ci;

    function __construct()
    {
       $this->ci =& get_instance();
    }

    function do_something($array_a)
    {
        $array_b = $this->do_something_else($array_a);
        return $array_b;                  
    }

    function do_something_else($thearray)
    {
        $return $thearray;
          // doesn't do much, just demonstrating.
    }

}

Then in a controller:

Code:
$this->load->library('mylibrary');

$array = array( 'test' => 'value');
var_dump($this->mylibrary->do_something($array));

That should work as far as I can determine.
#7

[eluser]InsiteFX[/eluser]
Hi,

Try this using a private method.

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mylibrary{

    function do_something($array_a)
    {
       $array_b = $this->_do_something_else($array_a);                  
    }

    function _do_something_else($thearray) // use private method
    {
          $return $thearray;  // doesn't do much, just demonstrating.
    }

}

Enjoy
InsiteFX
#8

[eluser]SneakyDave[/eluser]
Yes, thanks! That worked exactly as it should. I didn't think the CI private methods worked in libraries, just controllers, but I guess I learned something today.
#9

[eluser]InsiteFX[/eluser]
Your very welcome!

Enjoy
InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB