0

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.

1
  • "Argument two is the password."? How to detect what's nth value in an associated array in php? I think it's not possible (it does sound absurd). Am I right? Commented Aug 30, 2012 at 20:12

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Ah, I see. Is there a reason you need to pass individual params? Is that just your own personal reason or is there a technical one?
It's because I want to be able to reuse this class outside of CI solutions. Aka. I have other solutions that also rely on this class... but they instantiate the "traditional" way...
Is there a reason you couldn't pass an array in said other solutions?
no... other than the fact that i don't want to have to go back and change code that's currently working.
0

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

9 Comments

Brendan, thanks for the suggestion. But that doesn't seem to work. getting the message: Fatal error: Call to a member function connect() on a non-object in /var/www/myapp/application/models/my_model.php on line 84
I left in $classname for illustrative purposes, but you should use the classname in its place, i.e. hp5406_ssh. Remember the reference will be in all lowercase, so you might be able to use $classname = strtolower($classname); and retain use of $classname.
hey brendan...i wanted to make this dynamic so I need to keep $classname as a variable, instead of hardcoding it. the thing is, the class name will be passed in to me as an argument.
I guess I will have to revisit this design if I cannot use a varialbe for the class name...
So use strtolower so that it CI can use it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.