Autoloaded Classes that Receive Values During Instantiation |
[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.
[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
[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 { InsiteFX
[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) { 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.
[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
[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(); 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) Hah thought that was pretty funny. |
Welcome Guest, Not a member yet? Register Sign In |