0

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...

8
  • Pass the variables to the constructor as parameters when instantiating the class. Commented Aug 30, 2012 at 20:52
  • can you be a little more specific? for example, i already know about doing something like: $params = array('_hostname' => $ip, '_password' => 'password', '_username' => ''); $this->load->library($classname,$params ); //switch model name must be capitalized. Commented Aug 30, 2012 at 20:55
  • 1
    I think you are going to use it outside of codeigniter, so in that case you may instantiate it using $abc=new ABC($var1, $var2, $var3, $var4);, not sure if I understood it already. Commented Aug 30, 2012 at 20:59
  • @zerkms. yes, there is a technical reason why. and that is that other solutions - non codeigniter solutions- will be using this library and sending an array of parms is not the traditional way of instantiating a class with certain values set. hope that's technical enough. Commented Aug 30, 2012 at 23:27
  • @Sheikh Heera - you are correct. I am planning on reusing this library in non CI solutions. after doing more reading.. I'm thinking that that's basically the way i'm going to have to go. just do an include / require and take it from there. thanks. Commented Aug 30, 2012 at 23:28

2 Answers 2

2

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.

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

1 Comment

Thanks! I like that a lot better!
1

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.

Comments

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.