Welcome Guest, Not a member yet? Register   Sign In
[Q] cannot pass arguments to __constructor with load->library
#1

[eluser]vlad_ci[/eluser]
Hello
I have moved up to the latest CI (from 1.6.3)
and now writting a Java client to access my web serivces that are in PHP/CI

on a POST call from my Java client,
I am getting a strange error that I think I donot get when calling the same method from AJAX Web browser
(or at least I am not seeing the error ...)

The error is in the fact that I defined a class (this is all PHP 5.2.9/win)
the I am trying to load it with
CI->load->library


$args=1;
$this->load->library('CUsr_BaseData', $args);


--- class ---

class CUsr_BaseData
{
function __construct ($arg ) // <---- error here saying
{

/*
<p>Severity: Warning</p>
<p>Message: Missing argument 1 for CUsr_BaseData::__construct(), called in C:\usr\local\xampp\htdocs\codeigniter\system\libraries\Loader.php on line 931 and defined</p>
<p>Filename: libraries/CUsr_BaseData.php</p>
<p>Line Number: 41</p>


*/
$mydbg= new CMyDebug (__CLASS__,__METHOD__);

}

-----------




and I am getting a PHP error saying that the argument of the constructor is not given
It also says that the 'calling' code in

CI's Loader.php is not passing the argument (line 931). And indeed when I look at the code
there it is not passing any arguments to my class constructor. I assume I am causing somehow CI
to go to this code path and it should go somewhere else instead.

function _ci_init_class($class, $prefix = '', $config = FALSE, $object_name = NULL)
{.....

$CI =& get_instance();
if ($config !== NULL)
{
$CI->$classvar = new $name($config);
}
else
{
$CI->$classvar = new $name;
}

}


I do not have any configuration in /config folder for this library


So I am at a loss why it is happening. I checked the _$REQUEST array
and all the key-value pairs I am passing with my POST are there, the session id is there

Does CI rely on something that may be my client is not doing, or why is this happening?


Thank you in advance,
Vlad
#2

[eluser]Dam1an[/eluser]
Are you maybe autoloading the library as well? As that would be called with no args before you get to you manually loading it
#3

[eluser]Evil Wizard[/eluser]
it is possible to pass arguments to the library classes, however the arguments have to be passed in as an array

controller snippet...
Code:
...
    $this->load->library('library_class', array('arg1', 'arg2' => true), 'library_object_name');
...

library snippet...
Code:
...
public function __construct($arrArgs)
{
    print_r($arrArgs);
}
...

the loader class creates a new instance of the library which suggest that multiple configured instances of the library should be available, but i suspect that CI updates the links to other objects with the current instance of the libraries.

an alternative to this would be...

controller snippet...
Code:
...
    $this->load->library('library_class');
    $this->library_class->initialize('arg1', TRUE);
...

library snippet...
Code:
...
public function initialize($arg1, $arg2)
{
    echo arg1;
    echo arg2;
}
...
#4

[eluser]Dam1an[/eluser]
Well spotted Evil, I just skimmed the code (partially cause it wasn;t in code tags) and Assumed the input was an array
#5

[eluser]vlad_ci[/eluser]
thank you
it works now
it looks like from 1.6.3 to 1.7.1 CI is enforcing the fact that it must be array




Theme © iAndrew 2016 - Forum software by © MyBB