I am making a library for CodeIgniter, and I wish to pass multiple parameters of different types (a PDO object, username and password, configurations, etc).
I know I can pass an array of all of these things, but that doesn’t seem to be the best way of doing things (as $params can’t ever describe what is needed).
How can I pass multiple parameters to a library?
Thanks in advance.
There are several approaches to this particular problem. I’ll list (in preferred order) ways I know to solve it:
Associative Array Arguments:
This approach is pretty flexible, as the order of the parameters doesn’t matter, and it resolves a pretty big complaint many have with how PHP defines function parameters. You simply pass in the “non-default” parameters you want. This is probably the most “codeigniterish” way to do it, if that’s even a thing.
Numbered Arguments:
This approach replicates the typical PHP parameter paradigm (defines names, orders, and default values for all expected parameters).
Override the CI Loader class:
This is AT YOUR OWN RISK. Basically, the
CI_Loader::_ci_init_class()method needs to be overridden with aMY_Loaderclass and corresponding method. These are the lines that you “don’t like” (lines 1003-1012 in my install):The “safest” replacement that I could guess would be this:
I really don’t know how many things this will break, but I can almost certainly say there will be something. It’s not really worth the risk. I’d STRONGLY recommend the first approach above.
Cheers!