dot
dot

Reputation: 15690

How to pass parameters to a class constructor - not an array

I read some other posts here on stackoverflow about passing parms to a class library (for example, How to pass multiple parameters to a library in CodeIgniter?) and I also read the user manual under the libraries section.

What I'm wondering is if it's possible to pass parameters as individual variables and not as an array?

My class - which I want to use outside of my codeigniter solution- look like this:

 <?php

     class ABC
     {


     private $_hostname;
     private $_password;
     private $_username;
     private $_connection;
     private $_data;
     private $_timeout;
     private $_prompt;


public function __construct($hostname, $password, $username = "", $timeout = 10) 

{
    set_include_path(get_include_path() . PATH_SEPARATOR . '/var/www/phpseclib');
    include('Net/SSH2.php');

    $this->_hostname = $hostname;
    $this->_password = $password;
    $this->_username = $username;

} // __construct

    public function connect()
    {
    }

    public function dosomething()
    {
    }

I know I can do something like :

   $params = array('_hostname' => $ip, '_password' => 'password', '_username' => '');
$this->load->library($classname,$params );

But I do not want to use arrays because this class will be reused by non CI solutions. I've tried reverting to using a standard include() statement. So the code looks like:

      set_include_path(get_include_path() . PATH_SEPARATOR . 'var/www/myCIapp/application/libraries');
      include_once('libraries/'.$classname.'.php');

But it's complaining saying that it cannot find the include file. The specific message is:

A PHP Error was encountered

Severity: Warning

Message: include_once() [function.include]: Failed opening 'libraries/HP5406_ssh.php' for inclusion (include_path='.:/usr/share/php:/usr/share/pear:var/www/myCIapp/application/libraries/')

Filename: models/ss_model.php

Line Number: 88

Any suggestions would be appreciated. Thanks for reading...

Upvotes: 0

Views: 627

Answers (2)

Josh Holloway
Josh Holloway

Reputation: 1683

A simple way of doing your include could be dome using the CI APPPATH CONSTANT:

include_once(APPPATH.'libraries/'.$classname.'.php';

This CONSTANT is set in the index.php by CodeIgniter so you know it'll always be pointing to your Application directory.

Upvotes: 2

dot
dot

Reputation: 15690

the solution to the include not working is to change the include statement to look like:

 include_once dirname(__FILE__) . '/../libraries/'.$classname.'.php';

I was assuming that it would automatically start with the location of the current file. But that's not true. The above line will force it to start from the location of the current file.

Upvotes: 1

Related Questions