Reputation: 15660
I had a model that had an unrelated class defined inside of it.
I was prototyping and didn't spend time to create it properly.
Now that the prototype was accepted by my end users, I'm revisiting and I would like to reorg my code properly. The class looks something like this:
<?php
class myclassABC
{
private $_hostname;
private $_password;
private $_username;
private $_connection;
private $_data;
private $_timeout;
private $_prompt;
public function __construct($hostname, $password, $username = "", $timeout = 10)
{
$this->_hostname = $hostname;
$this->_password = $password;
$this->_username = $username;
$this->_timeout = $timeout;
} // __construct
public function connect()
{
}
public function dosomethingelse()
{
}
}//end class
I've moved all this code into a separate file, and this file is now in my libraries folder. But i'm having problems figuring out how to properly instantiate an object in my model. I tried:
//pass all the data we need as an array of parameters.
$params = array('_hostname' => '$ip', '_password' => 'password', '_username' => '');
$hp = $this->load->library($classname,$params );
$hp->connect();
$data= $hp->dosomethingelse();
$hp->close();
It's loading the right class, but I'm getting the following error message:
Severity: Warning
Message: Missing argument 2 for HP5406_ssh::__construct(), called in /var/www/m.racktables/system/core/Loader.php on line 1095 and defined
Filename: libraries/HP5406_ssh.php
Line Number: 22
Argument two is the password.
Sorry, this is my first attempt at using libraries with codeigniter.
if you could provide some suggestions, it'd be appreciated.
Upvotes: 0
Views: 3986
Reputation: 15660
The problem is that the constructor was not expecting an array. Which is what I am passing, based on the example in the codeigniter manual under the libraries section. i changed my constructor to accept an array and now it works. I'm going to post another question to see how I can pass individual parms.
Upvotes: 1
Reputation: 4565
Instead of doing this:
$hp = $this->load->library($classname,$params ); //switch model name must be capitalized.
$hp->connect();
$data= $hp->dosomethingelse();
$hp->close();
Do this:
$this->load->library($classname, $params);
$classname = strtolower($classname); // get the classname to lowercase for CI
$this->$classname->connect();
$data = $this->$classname->dosomethingelse();
$this->$classname->close();
CodeIgniter loads the library class instance into $this
. It's kind of strange, but it's consistent with the way the rest of CI works. If you don't like this method, you can always just include()
or require()
the class and use it the 'normal' way.
See the documentation for Creating Libraries.
Here's the relevant portion:
From within any of your Controller functions you can initialize your class using the standard:
$this->load->library('someclass');
Where someclass is the file name, without the ".php" file extension. You can submit the file name capitalized or lower case. CodeIgniter doesn't care.
Once loaded you can access your class using the lower case version:
$this->someclass->some_function(); // Object instances will always be lower case
Upvotes: 0