CodeIgniter Forums
Passing a second variable into a custom library - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Passing a second variable into a custom library (/showthread.php?tid=48813)



Passing a second variable into a custom library - El Forum - 01-29-2012

[eluser]WoolyG[/eluser]
Hi,
I have a custom library as follows:

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

class Someclass_library {

    public function __construct($key, $options = array())
    {
      //Do awesome stuff
    }
}

/* End of file Someclass.php */


I'm using the following, which works:

Code:
<?php
$someclass_data = array("key" => "mykeygoeshere");
$this->load->library('Someclass_library', $someclass_data);



What I'm trying to do is to utilise the second parameter of the __construct. How do I do this? I'm trying the following, but it's not working:

Code:
<?php
$someclass_data = array("key" => "mykeygoeshere");
$option = array("application" => "myapplication");
$this->load->library('Someclass_library', $someclass_data, $option);


.. can anyone shed any light on how to access the second parameter of a __construct() when loading a custom library?

Thanks!
WoolyG



Passing a second variable into a custom library - El Forum - 01-29-2012

[eluser]vbsaltydog[/eluser]
http://ellislab.com/codeigniter/user-guide/libraries/loader.html

The first parameter is the library, the second is the array of options, the third is an optional object name.

Take your key and other options, put them all in a single array, pass that array as the second parameter, then array_shift the key off of the array in the library constructor.




Passing a second variable into a custom library - El Forum - 01-30-2012

[eluser]WoolyG[/eluser]
Thanks for that. For what it's worth, a clear method of doing what I need to do is outlined in the first reply to this StackOverflow question:

http://stackoverflow.com/questions/3480633/passing-arguments-when-loading-custom-codeigniter-library



I was thinking the User Guide (http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html) might be a little unclear on how to pass multiple parameters to the custom library's __construct..


I'll put forward a suggestion to possibly have it clarified. Thanks.