How to avoid class name collisions in application/libraries classes? |
[eluser]sherbo[/eluser]
Hey All, I have a bunch of classes in /system/application/libraries with various interdependencies. At the top of each file I have a bunch of Code: require_once I'm trying to call a custom /system/application/libraries/Session_Management class function once during startup. I did this by adding the following to BASEPATH/index.php: Code: /** SET UP OUT CUSTOM SESSION MANAGER ****************************************/ When I do this, however, I get the following error: Code: Fatal error: Cannot redeclare class ... some_class_in_/system/application/libraries Is there a better way to call my /system/application/libraries/Session_Manager class? What's the best practice for specifying library dependencies in CI and PHP in general? I'm new to PHP and can't decide what's the best between include, require, require_once, and _autoload(). Class name collisions seem possible in each. cheers, Sherban
[eluser]sherbo[/eluser]
BTW, I Googled this ... --------------------------------------------------------------- From fastest to slowest: Code: include Why? Code: include Code: require Code: include_once Code: require_once Does this mean I should always use require()? Isn't require_once() more memory-efficient?
[eluser]xwero[/eluser]
The CI way is to load the library using Code: $this->load->library('Session_Manager'); I see it uses an init the best way to deal with that is to call/move the init in your constructor if you want to autoload your library. If you load the library you have the possibility to add params to using the second argument of the load->library method. But then your constructor needs to accept an argument too Passing Parameters When Initializing Your Class
[eluser]sherbo[/eluser]
The CI term for "loading a library" is misleading. It's instantiating a class, not "loading" it for possible reference. Also, it forces your library to share a common constructor signature. What if your library has only static methods? Why would you need to "load" (aka instantiate) it? There's no way to reference a CI library w/o instantiating it apart from using "require" statements. To me, CI's library "loading" is very inelegant.
[eluser]xwero[/eluser]
why don't you need to instantiate a class with only static methods?
[eluser]sherbo[/eluser]
How would I reference a a custom library class in the /application/libraries/ directory without doing this? Code: $this->load->library('Session_Manager'); The only other alternative is: Code: require_once 'Session_Manager'; |
Welcome Guest, Not a member yet? Register Sign In |