Welcome Guest, Not a member yet? Register   Sign In
Autoloaded Classes that Receive Values During Instantiation
#11

[eluser]Krumpet[/eluser]
Thanks for weighing in, theprodigy. Your comment makes sense.

Are you able to confirm all Library classes can only accept arrays as input? I haven't been able to find this statement in the CI docs.
#12

[eluser]theprodigy[/eluser]
http://ellislab.com/codeigniter/user-gui...aries.html
Look for the heading:
Passing Parameters When Initializing Your Class
First sentence says:
Quote:In the library loading function you can dynamically pass data as an array via the second parameter and it will be passed to your class constructor
#13

[eluser]InsiteFX[/eluser]
Unless the Reactor Team has chnaged it Libraries are the only one that will accept an array for parameters.

You can do it with a Model if you create an Intialize method and pass the array into it.
Code:
class Model_name extends CI_Model {

    private $options = array();

    public function initalize($params = array())
    {
        $this->options = $params;
    }
}
If you look at the system/core/loader.php libraries you will see how they are doing it.

InsiteFX
#14

[eluser]Josh Bright[/eluser]
I might be off base here a bit but, when you load a library, it runs its constructor. So, when you did the $this->load->library("fetch"), you need to pass in some data if you want to set that when it loads, (something like, $this->load->library("fetch", $params). Since you didn't pass anything in after $this->load->library("fetch"), that's why you get the error saying that its missing the first parameter.

I don't really like how that works, so, normally when I build a library I make some methods to set prefs after its been autoloaded. So, something like:

Code:
public function setPrefs($prefs) {
     if (is_array($prefs)) {
          //Do some array stuff
     } else if (is_object($prefs)) {
          //Do some object stuff
     } else {
          die("Not sure what I received");
     }
}

That way you can autoload your library, and set the prefs later, without having to worry about sending it that data when you load the library itself.
#15

[eluser]InsiteFX[/eluser]
I belive the library only accepts an array, you can see this by looking at system/core/loader.php libraries

So if you want to pass a string or integer just assign them to the array, the array can have mixed values.

Also you will need to grab the array in your constructor and assign to a public or private array, the same thing for pulling a string or integer out of the array.

InsiteFX
#16

[eluser]Josh Bright[/eluser]
Still, by not requiring the prefs to be passed to the constructor, and having a separate method to do that gets around this problem, if one needs to use an object/string/integer for whatever reason.

Code:
$prefs = new stdObject();
$prefs->name = "Test Pref";
$prefs->type = "normal";

$this->load->library("my_library");
$this->my_library->setPrefs($prefs);

If you need to send just an integer or string into a library, you may as well just wrap that in an array, or, use a setter in your library to set that one value. It just sort of seems like to me an unnecessary thing (dealing with handing data to a library in the load->library() method) to have to think about when dealing with libraries when you can just make your own setPrefs type method to handle the data (whatever type) however you want.

On a side note, in looking around in the Loader.php file (in system/core), i found this right below the library method:

Code:
function model($model, $name = '', $db_conn = FALSE)
    {
        if (is_array($model))
        {
            foreach($model as $babe)
            {
                $this->model($babe);
            }
            return;
        }

Hah thought that was pretty funny.




Theme © iAndrew 2016 - Forum software by © MyBB