Welcome Guest, Not a member yet? Register   Sign In
passing an argument to a library constructor doesn't work anymore with ci 1.7 :(
#1

[eluser]oll[/eluser]
Hello here,

I don't know if it's a normal new behaviour or if I did something wrong upgrading to ci 1.7, but my code doesn't work anymore :

Controller :

Code:
<?php
class Test extends Controller {

        function Test()
        {
                parent::Controller();
        }

        function index()
        {
                $this->load->library ('testlib',"Tom Sawyer");
        }
}
?>

Library :

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

class Testlib {

        function Testlib($user)
        {
                echo "hello $user";
        }

}
?>

The error is :

Code:
Severity: Warning

Message: Missing argument 1 for Testlib::Testlib(), called in /var/www/html/videos/ci/system/libraries/Loader.php on line 931 and defined

Filename: libraries/testlib.php

Line Number: 5
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: user

Filename: libraries/testlib.php

Line Number: 7


When I downgrade back to ci 1.6.3, it works again Sad
I have the Hello Tom Sawyer message with no errors.

I checked the changelog and especially everything about the new library loader but I didn't find a way to solve my problem.
I did a search in the forum but didn't find a similar problem neither.

Thank you.
#2

[eluser]oll[/eluser]
I tried with a fresh install of ci 1.6.3 and 1.7 , and I can reproduce this problem. (It works with ci 1.6.3 but not with 1.7)

I solved the problem passing an array, instead of a single variable.
So this is a new behaviour.
Maybe I was "lucky" that it worked with 1.6.3, but the doc doesn't say that the params MUST be an array.
#3

[eluser]Unknown[/eluser]
I also ran into this change when upgrading to 1.7.

Using 1.6.3 I had the following, where $this->website is a string.

Code:
$this->CI->load->library('fp_create_database',$this->website);

Code:
function __construct($dbname) {
        parent::__construct();

        $dbname = split(':',$dbname);
        $this->dbname = $dbname[0];
        
}

Using 1.7 I had to change the code to pass in an array.

Code:
$this->CI->load->library('fp_create_database',array('dbname' => $this->website));

Code:
function __construct($dbname) {
        parent::__construct();

        $dbname = split(':',$dbname['dbname']);
        $this->dbname = $dbname[0];
        
}

Documentation states that
Quote:The second parameter allows you to optionally pass configuration setting. You will typically pass these as an array:

I could also not find anything in the Changelog referring to this change. Got me so I hope this post helps those with the same issue.

Thanks MJ
#4

[eluser]Unknown[/eluser]
Thanks a lot!
I was having the same issue with one of my old custom libraries breaking.
Switching the parameter to an array fixed it.




Theme © iAndrew 2016 - Forum software by © MyBB